Sub topic 16
Cost, latency and when not to use an agent
This chapter assumes you have read the earlier chapters on the agent loop, step budgets and evaluation. It covers what an agent costs, how to reduce that cost, and when classic RAG is the better choice.
Where the cost comes from
Classic RAG makes one retrieval and one model call. An agent makes several model calls, and each call re-sends the whole conversation so far. Input tokens therefore grow with every step, not just once.
Latency adds up the same way. Each step waits for a model call, then a tool call, then the next model call. Steps run in sequence, so a five-step run takes roughly five times as long as one call.
A rough cost model
Suppose a system prompt and question take 500 tokens, and each tool result adds 1,500 tokens.
| Design | Model calls | Input tokens sent (approx.) |
|---|---|---|
| Classic RAG | 1 | 2,000 |
| Agent, 1 search | 2 | 500 + 2,000 = 2,500 |
| Agent, 3 searches | 4 | 500 + 2,000 + 3,500 + 5,000 = 11,000 |
Three searches cost about five times more input than classic RAG. Output tokens and tool time come on top of that.
Ways to keep it down
- Cap steps. Keep
MAX_STEPSsmall. Raise it only if your evaluation shows questions that fail because of the limit. - Limit result size. Lower
top_kand truncate passages, assearch_docsdoes. - Drop stale results. Remove or summarize old tool outputs once they are no longer needed.
- Allow parallel calls. If the model asks for several independent searches in one reply, run them together. This saves wall-clock time, though not tokens.
- Use a smaller model for routine steps. A cheaper model can rewrite queries or grade results. Keep the stronger model for the final answer.
- Cache. Cache identical queries and their results.
When classic RAG is the better choice
Choose classic RAG when these hold:
- Almost every question needs retrieval, and one search usually answers it.
- Questions are narrow and come from one source.
- You need predictable latency or a hard cost ceiling.
- You need results that are easy to reproduce and debug.
Choose an agent when questions are multi-part, need several sources, or often need a second search with a better query. Agents also help when many questions need no retrieval at all.
A middle path is classic RAG with one fallback. Run the fixed pipeline first, grade the result, and start the agent loop only when the grade is poor. Most questions stay cheap, and hard ones get extra effort.
Exercise
Exercise
An agent starts with 600 input tokens. Each of its 3 searches returns 1,000 tokens, and the model is called 4 times (3 calls that ask for a search, then 1 that answers). Estimate the total input tokens sent across all calls. Classic RAG with one search would send 1,600 tokens. How many times larger is the agent's figure?
Show answer
Call 1 sends 600. Call 2 sends 600 + 1,000 = 1,600. Call 3 sends 600 + 2,000 = 2,600. Call 4 sends 600 + 3,000 = 3,600. The total is 600 + 1,600 + 2,600 + 3,600 = 8,400 tokens. That is 8,400 / 1,600 = 5.25 times the classic RAG figure. This ignores the tokens the model itself adds each step, so the real figure is slightly higher.
Exercise
Your support bot gets short questions about one product manual, and nearly all are answered by one passage. Latency must stay under two seconds. Should you use an agent? Give a reason and one compromise.
Show answer
Classic RAG is the better fit. One retrieval answers most questions, and extra model calls would threaten the latency limit. A compromise is to run classic RAG first, grade the retrieved passages, and call the agent loop only when the grade is low.