Lesson 3 of 3 Optimizing Duration, Context and Tokens

Optimizing Token Usage in Multi-Agent Systems

This lesson covers token limits, loop controls and tool-call optimization, and how to measure their effect, since tokens are the unit of both cost and latency and multi-agent systems multiply them.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify where tokens go in each call.
  • Apply token limits and loop controls at several levels.
  • Make tool calls leaner.

Where tokens go

Input tokens in each call include the instructions, the definitions of every tool available to the agent, the conversation or state, retrieved passages and previous tool results. Output tokens include the visible answer, tool-call arguments and, for reasoning models, reasoning tokens that are billed as output but not shown. In an agent loop, input tokens are reprocessed on every iteration, so a large tool result early in the run is paid for many times.

Fill in the number

Tokens paid many times

Exam skills 3.3.3

A 6,000-token tool result arrives in the first iteration of an agent loop and stays in the context.

Log in to try this activity Interactive activities are free with an account.

Token limits and loop controls

Token limits operate at several levels: max_output_tokens on each call, a token budget per run and per subagent enforced by the orchestrator or middleware, per-user or per-team quotas, and the API Management token limit policy, which enforces tokens per minute and quotas per consumer at the gateway. Loop controls cap the number of iterations, tool calls and subagent spawns per run, detect repeated identical tool calls, and end the run with the best available answer when limits are reached.

Match the pairs

Token limits at each level

Exam skills 3.3.3

Match each level with the token control that applies there.

Log in to try this activity Interactive activities are free with an account.

Leaner tool calls and other techniques

Tool calls are often the largest source of waste. The following practices reduce it.

  • Return only needed fields, paginate and summarize large results inside the tool.
  • Expose only the tools a step needs; every definition costs input tokens on every call.
  • Keep tool descriptions concise but precise.
  • Drop raw tool outputs from the context once their facts are extracted.
  • Cache deterministic tool results instead of calling again.

Other techniques include prompt caching for stable prefixes, compaction of history, smaller models and lower reasoning effort where quality allows, and passing structured state instead of transcripts between agents. Token usage per agent and per step is read from trace spans (GenAI usage attributes) before and after each change, together with quality scores, to confirm that savings do not reduce quality.

Scenario questions

Cutting tokens without cutting quality

Exam skills 3.3.3

Choose how to reduce and verify token usage.

Log in to try this activity Interactive activities are free with an account.
Worked example

An expensive agent loop

Traces show that a research agent averages 14 iterations per run. Each iteration's input includes 35 tool definitions and the full text of every web page fetched so far. Quality is acceptable, but cost is four times the target.

Three changes address the main sources: expose only the 5-6 tools the research step uses, have the fetch tool return a summary and key passages instead of full pages (with full text stored for retrieval on demand), and cap iterations with stall detection. Each change is measured from span token counts and checked against quality scores. Switching to a cheaper model alone would leave the waste in place.

Check yourself

Which changes usually save the most tokens in an agent loop?

Show answer

Fewer tool definitions and trimmed tool results, because both are reprocessed on every iteration.

Exam Tips

Remember for the exam

  • Input tokens are reprocessed on every loop iteration; big early results are paid many times.
  • Enforce budgets per call, run, subagent and consumer (APIM token limit policy).
  • Fewer tool definitions and trimmed tool results usually save the most.