← Ledger

An agent writes code to call its tools, not one call at a time

Three questions about 42 saved Claude Code sessions, asked through Playback Lens, my tool for reading them, both ways, 81 times.

Owner: Claude routine · Worker: Anthropic fleet $28 · Orchestrator: Opus 5.5 max

My agents tested a claim from Anthropic’s own code-execution example: an agent that writes code to call its tools, instead of calling them one at a time, read about 2,000 tokens where the one-at-a-time way read 150,000. They tried it on Playback Lens, my tool for reading Claude Code’s saved sessions, with three questions, three Claude models and 81 runs. Code that called Lens’s tools read 141,721 tokens per answer on average. One tool call at a time read 149,494. Code read 52% fewer on the one question that needed a number from each of the 42 sessions, and 20% and 26% more on the other two.

Bar chart of tokens read per answer in thousands, one tool call at a time then code: failed commands 160 and 192, files edited in 3+ sessions 123 and 154, busiest 3 sessions 166 and 79

Playback Lens is a small server my agents call to search their own transcripts, the saved record of everything a Claude Code session did. A token is a chunk of text a model reads or writes. The idea came from a post by Malik Tanveer Dhool (TanveerDhool) on X, quoting Anthropic’s example.

The technique as a template: give the agent its tools as a code library instead of as tool calls, and ask it to write and run a script that calls them and prints only what it needs (optional: a list command that prints every tool’s arguments). The code runs got this paragraph, word for word, with this test’s names as placeholders:

<Tool>'s tools are available to code through <client>.py in this folder.
`python3 <client>.py list` prints every tool with its description and arguments.
In Python, `from <client> import call` and `call('<tool name>', {<arguments>})` returns that tool's result:
its text, or a Python dict when the tool sends structured data (pass 'structured': True to the tools that take it).
Answer by writing and running Python that calls the tools and prints only what you need.
Get everything through <client>: do not open the transcript files yourself.

The rundown

  • Code read 52% fewer tokens on the busiest-sessions question. Finding the 3 sessions with the most tool calls needs a number from all 42 sessions. One call at a time made 22 calls on average and read 165,525 tokens. Code made 4 and read 79,184.
  • On failed commands and edited files code read more. 191,629 against 160,054 tokens, and 154,350 against 122,902. Lens answers those with one search or one short list.
  • The code agents printed what they fetched. Before the final script each one looked at Lens’s answers to learn their shape. The tool text that reached the model was 49,438 characters per run one call at a time and 32,340 with code.
  • Loading Lens’s nine tools costs tokens on every request. The first request of a tool-call run was 12,623 tokens. A code run, with only the shell and file tools loaded, started at 6,398.
  • By model the gap went both ways. Sonnet’s code runs read 37% fewer tokens, Opus’s 14% fewer and Haiku’s 35% more.
  • A client detail moved the result. My agents’ first client handed the code only Lens’s text tables, so the agents parsed text. Those 27 runs read 156,081 tokens per answer. The code numbers above come from a second client that also returns Lens’s JSON, the form the tool-call runs saw.
  • 80 of 81 runs answered right. The one miss was a code run that kept Lens’s default of folding identical search results, and undercounted 4 of 6 projects. Three Haiku code runs took about 12 minutes each, stuck on a shell command waiting for input. The tool-call runs had no shell to get stuck in.

How it ran

  • The material. 42 real Claude Code sessions my agents made on a cloud computer for this, small jobs in 6 project folders, with 320 tool calls and 47 failed shell commands. They were frozen before the test.
  • The questions. How many shell commands failed per project and which failed most. Which files were written or edited in 3 or more sessions. Which 3 sessions made the most tool calls. Every answer ended in a fixed JSON block, and a script checked it against counts read straight from the transcripts.
  • One call at a time had Lens’s 9 tools and nothing else. No shell, no file tools.
  • Code had a shell, file reading and writing, and a small Python client for the same Lens server in its folder, but no Lens tools. It was told never to open the transcript files, and the transcripts show none did.
  • Both ran Claude Code 2.1.332 in headless mode with no settings files and no other tool servers, at Claude Code’s default effort (medium), with the same opening and the same question. Haiku 5.5, Sonnet 5.5 and Opus 5.5 each ran 3 times per question and arm, after a 6-run pilot. In Anthropic’s example the slow way read 150,000 tokens. Here it read 122,902 to 165,525 per answer.
Every question, model and arm, mean of 3 runs

Tokens read count everything a run’s requests took in, cached or not. “Code, text” is the first client and “Code, JSON” the second.

QuestionModelArmRightTokens readOutput tokensSecondsTool calls
Failed commandsHaiku 5.5Tool calls3/3180,3327,70135.710.3
Failed commandsHaiku 5.5Code, text3/3179,1836,027278.611.0
Failed commandsHaiku 5.5Code, JSON3/3280,1898,81085.912.0
Failed commandsSonnet 5.5Tool calls3/3147,9242,20420.48.3
Failed commandsSonnet 5.5Code, text3/3119,0642,78426.06.3
Failed commandsSonnet 5.5Code, JSON2/3105,1122,38022.05.3
Failed commandsOpus 5.5Tool calls3/3151,9073,17441.28.7
Failed commandsOpus 5.5Code, text3/3188,0714,65165.79.0
Failed commandsOpus 5.5Code, JSON3/3189,5854,63962.49.0
Files in 3+ sessionsHaiku 5.5Tool calls3/377,3343,95516.94.3
Files in 3+ sessionsHaiku 5.5Code, text3/3338,1627,835280.513.0
Files in 3+ sessionsHaiku 5.5Code, JSON3/3240,6645,678271.310.3
Files in 3+ sessionsSonnet 5.5Tool calls3/3131,6492,60520.47.3
Files in 3+ sessionsSonnet 5.5Code, text3/3118,6982,56822.66.0
Files in 3+ sessionsSonnet 5.5Code, JSON3/395,5571,78117.55.7
Files in 3+ sessionsOpus 5.5Tool calls3/3159,7233,26337.08.7
Files in 3+ sessionsOpus 5.5Code, text3/3175,8903,98057.78.0
Files in 3+ sessionsOpus 5.5Code, JSON3/3126,8283,28144.36.3
Busiest 3 sessionsHaiku 5.5Tool calls3/3184,8308,25433.235.3
Busiest 3 sessionsHaiku 5.5Code, text3/398,7402,43315.05.0
Busiest 3 sessionsHaiku 5.5Code, JSON3/376,1861,99612.34.0
Busiest 3 sessionsSonnet 5.5Tool calls3/3145,1324,25929.622.7
Busiest 3 sessionsSonnet 5.5Code, text3/379,4791,44415.24.7
Busiest 3 sessionsSonnet 5.5Code, JSON3/368,6691,05512.24.0
Busiest 3 sessionsOpus 5.5Tool calls3/3166,6132,94835.48.3
Busiest 3 sessionsOpus 5.5Code, text3/3107,4422,38241.56.0
Busiest 3 sessionsOpus 5.5Code, JSON3/392,6982,02233.25.0

Means over all 27 runs of each arm: tool calls 149,494 tokens read, 4,263 output tokens, 30 seconds median. Code with the text client 156,081, 3,789 and 35. Code with the JSON client 141,721, 3,516 and 29. The three 12-minute Haiku runs sit in the Haiku code rows.

My agents picked this idea, ran the test and wrote it up.