For developers · Agent engineering

Your agent read 2 of 11 records and reported success

Capping tool results at a fixed number of characters breaks JSON and hides what was dropped. Here is what we do instead, and the open-source library that does it.

One of our agents called a search API, and the API returned 11 companies in 77,539 characters. Our agent loop capped tool results at a fixed size, 12,000 characters, and kept the start. The cut landed in the middle of a record:

…"name": "content delivery networ

The model parsed the two whole records it could see, wrote two rows to a spreadsheet, and reported success. Nothing in the result said nine records were missing.

Why it happens

Tool results are sent to the model again on every later turn of the loop, so a large one is paid for again and again. Capping them is reasonable. Capping them by the first N characters is not: it bounds bytes, not what the agent consumes. It produces broken JSON, it favours whatever came first, and above all it is silent. The model has no way to know the result was partial, so it treats it as complete.

What to do instead

In that incident the task needed 14 fields per company. With only those fields, all 11 records fit the same budget several times over. That points to an order of operations:

  1. Keep only the fields the task needs, when the caller knows them.
  2. If it still does not fit, drop the widest fields across all items before dropping any item, so every record survives in a narrower form.
  3. If it still does not fit, cut between whole items, never inside one.
  4. Tell the model, in the result itself, what it got and what it did not: how many items it received, how many exist, which fields went, and how to ask for the rest.

The last step matters most. When items do have to go, the result says so in plain words:

"items_included": 7, "items_available": 25, "complete": false,
"warning": "You did NOT receive all items. Do not describe this result as
  complete, and do not report the items you can see as if they were the
  whole set. To get every item, call this tool again with the SAME
  arguments plus `select=` naming only the fields you need …"

A model that reads that behaves very differently from one handed a truncated string.

The library

We took this logic out of our production agent runtime, where it handles every tool result, and published it as a small Python library with no dependencies: agent-tool-result-reducer.

pip install agent-tool-result-reducer
from tool_result_reducer import reduce_tool_result

rows = [{"id": i, "name": f"Company {i}", "description": "long text " * 400}
        for i in range(25)]
response = {"results": rows, "total": 443}   # about 100,000 characters

result = reduce_tool_result(response, "crm.search", budget=4_000)
result.summary("crm.search")
# crm.search: complete, agent received 25 of 25 items
#   (source reports 443 in total), dropped fields: description

result.text is 1,506 characters of valid JSON with all 25 items. Its _meta block says the description field was dropped and how to get it back. It also says the source holds 443 in total, so these 25 are not the full set. Keep result.raw for your own storage; only result.text goes to the model.

In the agent loop it is one line, between running the tool and adding its result to the conversation. There are runnable examples for the OpenAI Responses API, the Anthropic Messages API and LangChain. We ran all three against live models, gpt-6-luna and claude-opus-5-5. In every run the model received all 30 of 30 items, and it asked for only the fields it needed without being told to.

When we swapped version 0.2.1 back into the production codebase it came from, that codebase's full test suite, 8,779 tests, passed unchanged.

Where it stops

  • Budgets are in characters, not tokens. A token budget is on the roadmap.
  • One item is always kept, so a single item larger than the budget still exceeds it.
  • It does not store payloads or answer follow-up queries. That is your runtime's job; the library tells the model what to ask for.
  • Text that is not JSON falls back to a clearly marked character cut.

What we would like to hear

If you run agents against real APIs: how do you bound tool results today, and would a token budget matter more to you than a character one? The feedback thread is open, and so are the issues.