Token LabBuscar

← Todos los tutoriales
GUÍA PARA DESARROLLADORES / 03

Depura un desbordamiento: presupuesto práctico para RAG

Divide la solicitud, reserva espacio para generar y localiza la mayor fuente de crecimiento.

Calcula el presupuesto completo

La solicitud no es solo el último mensaje. Incluye instrucciones del sistema, historial, documentos recuperados, texto del usuario y margen para mensajes o herramientas. Reserva también la salida.

El ejemplo asigna 8.096 de 8.192 tokens y deja 96. Un documento adicional de 500 tokens excedería el límite en 404. Son cifras didácticas, no especificaciones de un modelo.

Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

  • Understand: state assumptions, shapes/units and a counterexample.
  • Implement: reproduce a numerical example and inspect boundaries.
  • Investigate: compare one alternative under a matched budget.

Localiza la mayor contribución

Introduce cada sección por separado en Presupuesto de contexto. La herramienta la codifica y muestra su contribución. Carga el ejemplo que supera el límite para observar el aviso y la sección RAG dominante.

La codificación separada permite atribuir el uso, pero no reconstruye exactamente la solicitud final. Separadores, plantillas, esquemas de herramientas y medios pueden cambiarlo. Reserva margen y compara con el uso de la API.

context_limit = 8192
system        =  400
history       = 1800
retrieval     = 3600
user          =  120
extra         =  128  # estimate
output        = 2048  # reserve

allocated = 400 + 1800 + 3600 + 120 + 128 + 2048
remaining = 8192 - allocated  # 96 tokens
Probar en la herramienta →

Reduce sin romper la tarea

Si la recuperación es excesiva, elimina duplicados y reordena por relevancia; conserva los pasajes útiles y sus fuentes. Resume turnos antiguos sin perder decisiones y conserva el intercambio reciente.

Vuelve a contar y evalúa respuestas representativas tras cada cambio. No recortes silenciosamente instrucciones esenciales. Comprueba además el límite específico de salida: una ventana amplia no garantiza una respuesta de igual tamaño.

Referencias e implementación

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.

Shared code, English mathematical explanation and key multilingual numeric passages reviewed. This is not a full translation proofread.

System + history + retrieval + user → Add template estimate + output reserve → Compare allocated total with limit → Trim retrieval while retaining reserve
Mechanism map: follow the transformations from top to bottom.

Before running: predict a boundary case

8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node context-budget-rag.mjs. No third-party runtime dependencies.

const allocated=400+1800+3600+120+128+2048;
console.log(allocated,8192-allocated,allocated+500-8192); // 8096 96 404
console.assert(allocated+500-660+256===8192);

Test and transfer

8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope