Sub topic 8
Tool definitions and tool results
This chapter assumes you have called a model with tools (function calling) at least once. You should know what a system prompt is, what a token is, and how to read short Python code. It covers the two ways tools consume context: the definitions you send on every call, and the results that come back and stay in the history.
Tools cost tokens twice
A tool definition is the name, description, and parameter schema that tell the model what a tool does. The model only knows about a tool through this text. A tool result is the output your code returns after the model asks for a call.
Both occupy the context window. Definitions are paid on every call. Results are paid on the call that receives them, and again on every later call, because they become part of the history.
Writing definitions that earn their tokens
A definition has to be specific enough that the model picks the right tool and fills in valid arguments. It should not be longer than that.
- Name tools by what they do:
search_orders, nottool1. - State when to use the tool and when not to. One sentence on the boundary with similar tools prevents most wrong choices.
- Describe each parameter with its format and an example value when the format is not obvious.
- Remove tools that the current task cannot use.
Overlapping tools are a common cause of confusion. If find_customer and lookup_user both exist and a person could not say which to use, the model cannot either. Merge them or sharpen the descriptions.
def estimate_tokens(text):
# Rough heuristic: about 4 characters per token for English text.
return max(1, len(text) // 4)
tools = {
"search_orders": "Search orders by customer email. Use for order status. "
"Do not use for refunds. Returns at most 5 orders.",
"issue_refund": "Refund one order by order_id, for example 'A-1042'. "
"Use only after the user confirms.",
"export_all_orders": "Dump every order in the database as CSV.",
}
def select_tools(tools, allowed):
return {name: desc for name, desc in tools.items() if name in allowed}
task_tools = select_tools(tools, {"search_orders", "issue_refund"})
before = sum(estimate_tokens(d) for d in tools.values())
after = sum(estimate_tokens(d) for d in task_tools.values())
print(before, after)Choosing the tool set per task, as select_tools does, saves tokens on every call. It also removes options the model could misuse.
Controlling tool results
Results are often the largest items in the context. A database query can return thousands of rows and a web page can return tens of thousands of tokens. The model rarely needs all of it.
Three habits keep results under control:
- Return less by default. Add limits, filters, and field selection to the tool itself, such as
limit=5orfields=["id", "status"]. - Truncate with a signal. If you cut output, say so, so the model can ask for more.
- Clear stale results. After the model has used a result, replace it with a short note, such as "Result of search_orders: 5 orders, A-1042 is shipped." The history then keeps the conclusion and drops the bulk.
def shape_result(rows, max_rows, fields):
kept = [{f: row.get(f) for f in fields} for row in rows[:max_rows]]
note = ""
if len(rows) > max_rows:
note = f"Showing {max_rows} of {len(rows)} rows. Narrow the query to see others."
return {"rows": kept, "note": note}
rows = [{"id": i, "status": "shipped", "internal_notes": "x" * 500} for i in range(40)]
print(shape_result(rows, 3, ["id", "status"]))Common errors
- Sending every tool on every call, including ones the task never uses.
- Descriptions that repeat the tool name and add nothing.
- Returning raw API responses with unused fields and metadata.
- Error messages like
Error 500that tell the model nothing. Say what failed and what to try next. - Treating tool results as trusted instructions. A fetched web page can contain text that tries to direct the model. Handle results as data, as covered in the earlier chapter on context failures.
Exercise
A search tool returns 200 rows of 30 fields each, and the model needs only the order ID and status of the top 5. Name two changes that reduce context cost, and say where each should be made.
Show answer
Limit the rows to 5 and return only the id and status fields. Both changes belong in the tool code, before the result is sent to the model, so the extra rows and fields never enter the context. Add a note saying how many rows were left out so the model can request more.
Exercise
Why does a large tool result cost more than its size on a single call?
Show answer
It becomes part of the conversation history, so it is sent again, and counted against the token budget, on every later call until you trim, clear, or summarize it.