Sub topic 14
Managing the token cost of tools and results
This chapter assumes you know the tool-calling loop, how to write a tool definition (name, description, input schema), and how tool results flow back to the model. Here you will see where tool-related tokens go and how to reduce them without hurting tool selection.
Where the tokens go
Every request carries the full list of tool definitions as input. The model reads them each turn, and you pay for them each turn. Results add to this: a tool result is appended to the conversation and is re-sent on every later turn of the loop.
| Source | When you pay | What drives its size |
|---|---|---|
| Tool definitions | Every request | Number of tools, description length, schema depth |
| Tool results | Every later turn in the conversation | Fields returned, rows returned, text length |
| Error messages | Once, plus re-sends | Message length, retries |
Token counts vary by provider and tokenizer, so measure with your provider's token-counting feature instead of guessing.
Trim definitions without losing clarity
Description text is where selection quality comes from, so cut carefully.
- Remove words that repeat the schema. "A string" next to
"type": "string"adds nothing. - Keep the purpose, the "use this when" and "do not use this when" lines, and format hints such as
YYYY-MM-DD. - Merge tools that differ only by one parameter, if the merged description stays clear.
- Avoid large enums with hundreds of values in every request. Let the model search for a value with a tool instead.
- Pass only the tools relevant to the current task. A support agent for orders does not need the billing tools in an order-lookup step.
Shape results to be small
The model usually needs a few fields, not the whole database row. Return what it needs to answer or to make the next call.
def search_orders(customer_id: str, limit: int = 10) -> dict:
limit = max(1, min(limit, 50))
rows = fetch_orders(customer_id, limit + 1) # your data access
shown = rows[:limit]
return {
"orders": [
{"id": r["id"], "status": r["status"], "total": r["total"]}
for r in shown
],
"truncated": len(rows) > limit,
}This code does three things:
- It caps the row count with a bound that matches the schema's
maximum. - It returns three fields and drops internal columns.
- It sets
truncatedso the model knows more results exist and can narrow the search, for example with astatusfilter.
Silent truncation is worse than a large result, because the model will treat a partial list as complete.
Trade-offs
Cheaper is not always better. Cutting a "do not use this for..." line to save 15 tokens can cause wrong tool calls, and each wrong call costs a full extra turn. Shrinking a result so far that the model must call another tool also costs more. Use the evaluation loop from the testing chapter to confirm that accuracy holds after each cut.
Exercise
A tool returns 200 order rows with 30 fields each, and the model usually needs only the order ID and status. Name two changes that reduce token cost, and one signal you should add to the result.
Show answer
Return only the needed fields (id, status, and perhaps total), and cap the row count with a limit parameter that has a default and a maximum. Add a truncated flag so the model knows when more results exist and can narrow the search.