Skip to content

< CollectionsWriting effective tools for agents · 7 of 8 ·107 · Level 04, Tool Calling

Catch the Tool Call the Model Already Made

medium · implement · Tool Calling

A model that loses track of what it has done calls the same tool with the same arguments again. Implement CallGuard, which sits between the loop and the tools and notices. CallGuard(reads) takes the names of the tools with no side effects. guard.check(name, args) says what to do before a call runs, and guard.record(name, args, result) is called after one has.

  1. A call's signature is its name plus its arguments as canonical JSON with the keys sorted, so {"a": 1, "b": 2} and {"b": 2, "a": 1} are the same call, and {"a": 2} is a different one.
  2. A call not seen before returns ("run", None).
  3. A repeated read (a tool named in reads) returns ("cached", result) with the result that was recorded, and the tool does not run again. A result stays cached for the rest of the run.
  4. A repeated write (any other tool) returns ("refuse", message). The message names the tool, says it already ran with these arguments, and includes the recorded result, so the model can go on without it.
  5. The third refusal in a row for the same call returns ("stop", message): the model is stuck. The count is consecutive: any other call in between starts it over.

The catch. record is only called for calls that ran. A refused call is never recorded, so the result the message quotes is always the one from the call that did run.