AI agent latency and token budgets work best as one operating policy: set a deadline for the user-facing request, reserve time for each stage, and stop model or tool work when its remaining value no longer justifies its cost. This gives an agent a defined end state instead of relying on an outer timeout after it has already spent most of the request.
The goal is not to remove model-driven decisions. It is to make the duration, context growth, and external actions of those decisions controllable. For a production workflow, the budget must cover more than model generation: it also covers context assembly, tool execution, retries, orchestration, and the response or fallback returned to the user.
Define the request contract first
Begin with the workflow a user actually experiences. A short interactive answer, a support-case summary, and a long-running research operation should not inherit the same deadline simply because each uses a model. Define the expected completion mode before choosing technical limits.
For each workflow, document these request-level controls:
- End-to-end deadline: the point at which the workflow must return a completed result, a useful constrained result, or an explicit failure.
- Cumulative token allowance: the maximum input and output token usage permitted across the run.
- Turn limit: the maximum number of model-led decision cycles.
- Tool-invocation limit: the maximum external actions allowed for the request.
- Completion rules: what counts as a valid answer, when a partial answer is acceptable, and when the product must decline to complete an action.
These limits are product policy expressed in engineering terms. Their actual values should come from the service’s own traces, quality requirements, and dependency behavior. The important design choice is that every workflow has finite limits and a defined outcome when one is reached.
Anthropic distinguishes workflows with predefined paths from systems in which a model dynamically selects actions. That distinction is useful when setting budgets. Where a task can be represented as a known sequence, a workflow provides a more direct place to set and inspect limits. Where an agent loop is justified, its turns and actions need explicit boundaries. Anthropic’s engineering guidance also discusses starting with the simplest approach appropriate to the task.
Build a budget tree from the deadline
A request timeout alone does not make an agent predictable. A slow downstream tool can consume most of the available time, leaving no room to evaluate its result or compose a response. Unbounded retries have the same effect. Split the parent deadline into enforceable stage budgets, while retaining an overall absolute deadline.
| Stage | Budget to enforce | What to observe | Action when exhausted |
|---|---|---|---|
| Orchestration | Validation, routing, and queueing time | Start and finish time; selected path | Reject invalid work before invoking a model |
| Context assembly | Context size and preparation time | Selected inputs and their sizes | Keep higher-value context and remove lower-value material |
| Model call | Call deadline plus input and output limits | Duration and token usage | Use an approved constrained response path |
| Tool call | Tool-specific deadline, retries, and result size | Tool name, duration, retry outcome, result size | Skip optional work or report unavailable required work |
| Agent loop | Total turns, tool actions, and cumulative usage | Iteration count and termination reason | Stop the loop and choose the defined completion mode |
The stage allocation is not a universal template. It is a way to prevent one category of work from silently consuming the whole request. Pass the remaining absolute deadline to each adapter, then make a local decision about whether the next operation has enough remaining time to be useful.
Google Cloud’s generative AI guidance identifies input length as a latency consideration and recommends handling downstream dependencies with appropriate timeouts and retry strategies. This supports treating context and tools as first-class parts of latency planning rather than measuring only the final generation call. Vertex AI generative AI best practices
Separate input, output, and cumulative token limits
A single token limit obscures the control being applied. Input tokens constrain the context processed for a model call. Output tokens constrain the size of the generated artifact. A cumulative limit controls repeated calls across an agent run. Use all three when the workflow can call a model more than once.
Limit input before each request
Check the input allowance before constructing each model request. Account for instructions, the user request, tool definitions, retrieved material, prior turns, and tool results. If the request is too large, remove or compact lower-value material according to an explicit order. Typical candidates include duplicate retrieved passages, stale conversation content, verbose tool payloads, and metadata that does not affect the current decision.
Tool definitions belong in this calculation. Their descriptions and parameters are part of the material presented to the model. Clear, focused tool schemas can reduce unnecessary context and make available actions easier to distinguish. Anthropic’s guidance discusses keeping tools well defined and managing the model’s context deliberately. Building Effective Agents
Set output limits by artifact
Use a different output allowance for a routing decision, structured extraction, tool-selection turn, and final user response. A small internal classification result does not need the same generation budget as a customer-facing explanation. The application should request an output shape that matches the work being done, then set a corresponding maximum.
Stop on cumulative use, not only per-call use
Per-call limits do not prevent an expensive sequence of individually acceptable calls. Keep request-scoped totals for input tokens, output tokens, model turns, and tool invocations. Before another loop iteration, check all totals and the remaining deadline. Do not start a new model or tool operation merely because its individual limit is available if the parent request cannot use the result meaningfully.
Trace the full agent execution path
Aggregate model latency cannot explain a slow agent run. Operators need to see the request path: context preparation, each model call, each tool action, retries, and the reason the run stopped. Token usage and elapsed time should be correlated in the same trace so a slow request can be distinguished from a context-heavy request or a dependency-heavy loop.
OpenTelemetry Generative AI semantic conventions define vendor-neutral `gen_ai.*` telemetry for generative AI operations. The specification includes operation naming, input and output token usage, client operation duration, and tool-related attributes. Use the applicable conventions consistently so tracing data remains understandable when model providers or instrumentation libraries change.
A useful trace model includes:
- One root span for the user request, including the workflow identifier and final termination reason.
- A child span for each model operation, with duration and available usage information.
- A child span for each tool operation, with its identity, duration, outcome, and retry state.
- Events for context reduction, rejected next steps, fallback selection, timeout, retry, and budget-driven termination.
Keep sensitive data out of general-purpose telemetry. Prefer controlled identifiers, counts, sizes, and deliberately approved summaries over raw prompts, credentials, customer content, or unrestricted tool results. Trace design is part of the data-handling design of the service.
Make loop termination an orchestration responsibility
Tool loops combine model decisions with network dependencies. A tool result can lead to another decision, another tool request, and more context in the next turn. Asking a model to stop is not a sufficient operational control; the orchestration layer must decide whether another action is permitted.
Apply these controls to every loop:
- Cap sequential tool invocations for the parent request.
- Set tool-specific connection and read deadlines where the client supports them.
- Count retries against the same request time and invocation policy.
- Limit the size of tool results before they return to model context.
- Record why the loop ended: completed task, turn limit, tool limit, token limit, deadline, or tool failure.
LangChain4j documents events for AI service lifecycle observation, HTTP timeout configuration, tool-execution error handling, and limits for sequential tool execution. Those concepts provide useful integration points for Java services that need to identify and bound tool-driven execution. LangChain4j observability documentation
Use deliberate fallbacks
A fallback should be a product behavior chosen before an incident, not whatever remains after the outer timeout. Define which inputs can be reduced, which tools are optional, and which tasks may use an approved faster model. Keep the routing conditions specific enough to audit.
- Run the preferred path while enough time and token budget remain.
- Remove optional context or omit nonessential enrichment when a stage budget is reached.
- Use an approved alternate model only for task types whose quality requirements allow it.
- Return a constrained answer only when it is still grounded and useful.
- Fail explicitly when a required verification, write operation, or safety-relevant dependency did not complete.
Model tiering is discussed in Anthropic’s guidance as a way to match work to appropriate model capability. The policy should describe the eligible subtask and the required output, rather than using an unreviewable rule such as switching models whenever the request is slow. Anthropic’s model and workflow guidance
Apply the policy at the Java orchestration boundary
Keep request budget state in a request-scoped object owned by the orchestration layer. Model and tool adapters should receive the remaining deadline and applicable allowance, rather than independently choosing broad timeouts. This centralizes accounting and gives one policy layer authority to decline the next step.
Spring AI documents an advisor architecture around `ChatClient` calls, including observability support through Micrometer. Its tool-calling advisor participates in recursive tool-calling flows, making advisor interception a relevant place to observe calls and apply policy before a subsequent turn. Spring AI advisor documentation
Separate the budget policy from provider-specific execution code. Policy determines whether an action is permitted and what constraints apply. The adapter performs that approved operation with the provided deadline. This division keeps latency and spend controls stable as tracing backends, model clients, or agent frameworks evolve.
FAQ
What budget should an AI agent have first?
Start with an end-to-end request deadline and a maximum number of turns. They place an explicit boundary around two visible failure modes: waiting too long and continuing to decide on additional actions.
Should token limits be per call or per request?
Use both. Per-call input and output limits constrain an individual model operation. A cumulative request limit prevents repeated calls from exceeding the intended operational allowance.
How can a team diagnose AI agent latency?
Trace the full request and inspect context preparation, model operations, tool operations, retries, token usage, loop count, and termination reason together. This identifies whether delay came from model work, context growth, a tool dependency, or repeated orchestration.
When is a partial answer appropriate?
Only when the remaining result is grounded and useful for the user. If completion depends on a required verification, write operation, or unavailable system, identify that limitation rather than presenting an incomplete action as complete.
Sources
- OpenTelemetry Generative AI Semantic Conventions
- Spring AI Advisors Documentation
- LangChain4j Observability Documentation
- Anthropic: Building Effective Agents
- Google Cloud Generative AI Best Practices
Editorial note: AI assisted with research and drafting. Sources were selected for verification.
Full-Stack Developer & Solutions Architect · Casablanca, Morocco
7+ years building Java/Spring Boot/Angular enterprise solutions. Former Senior Software Engineer at NTT Data and Satec. Authorized Google Workspace and Microsoft 365 Partner for Morocco.