Sub topic 12
Shaping tool results the model can use
This chapter assumes you know the tool-calling loop: the model requests a tool, your code runs it, and you send the result back as the next message. It also assumes you can read short Python and JSON. The tool result is the only information the model gets about what happened. Design it as carefully as you design the input schema.
Why results need design
The model reads your result as text in its context window. It cannot see your database, your types, or your logs. If a result is cluttered, ambiguous, or huge, the model's next step suffers. A good result is small, labeled, and says what the model needs to decide next.
Return what the model needs
Return the fields that help the model answer the user or choose the next call. Leave out internal details.
- Include human-readable values. A status of
"shipped"helps more than"status_code": 3. - Include identifiers the model may pass to a later tool, such as
id. - Drop fields the model will never use, such as internal row versions, audit columns, and storage paths.
- Use stable, descriptive key names.
created_atis clearer thants.
The search_orders description in this tutorial promises "id, status, total, and created_at". The result should match that promise exactly.
RAW_ROWS = [
{"id": "ord_1001", "status": "shipped", "total_cents": 4250,
"created_at": "2026-11-02", "row_version": 7, "warehouse_shard": "eu-3"},
{"id": "ord_1002", "status": "pending", "total_cents": 1999,
"created_at": "2026-11-10", "row_version": 2, "warehouse_shard": "eu-1"},
]
def shape_orders(rows: list[dict], limit: int = 10) -> dict:
shown = rows[:limit]
return {
"is_error": False,
"orders": [
{
"id": r["id"],
"status": r["status"],
"total": round(r["total_cents"] / 100, 2),
"created_at": r["created_at"],
}
for r in shown
],
"returned": len(shown),
"total_matches": len(rows),
}
print(shape_orders(RAW_ROWS))The function converts cents to a decimal total, removes the internal fields, and reports both returned and total_matches. If those two numbers differ, the model knows the list is truncated and can narrow the search or tell the user.
Be explicit about empty results
An empty list is ambiguous. It can mean "nothing matched" or "the call failed quietly". Say which one.
{
"is_error": false,
"orders": [],
"returned": 0,
"total_matches": 0,
"note": "No orders matched. Try removing the status filter."
}A short note gives the model a next step without extra guessing.
Keep results bounded
Every token in a result is paid for on each later turn, because the conversation is resent. Set a hard cap, as limit does above. For large payloads, return a summary and an identifier for fetching details.
- Truncate long text fields and say so, for example
"description_truncated": true. - Prefer a count plus the top few items over a full dump.
- Format consistently, with the same keys on every call, so the model learns the shape.
Format
JSON works well for structured data. Plain text or Markdown works for prose such as a document excerpt. Pick one format per tool and keep it stable.
Exercise
A tool returns 10 orders, but 240 orders match the search. Which fields should the result include so the model does not wrongly tell the user they have only 10 orders?
Show answer
Include both returned (10) and total_matches (240). Seeing that the numbers differ, the model can say the list is partial and narrow the search, for example by adding a status or date filter.