A print shop must remind people who have not paid. It has rules on lateness, size, disputes and holds.
The first try gives the model tools and lets it work one call at a time. Each turn sends the whole history again. The tokens pile up.
The model writes one program that calls the tools. This is code mode.
- The model gets the tool names and writes a program.
- The sandbox runs it. A reminder is only written down there, not sent.
- A guard holds the whole rule: age, amount, disputes, holds, tone and repeats.
- Only reminders that pass are sent.
For example, the program loops over every bill without another model call.
sequenceDiagram
participant Code as Your code
participant Model
participant Runner as Sandbox
Code->>Model: the rules and the tool names
Model-->>Code: one program
Code->>Runner: run it
Runner-->>Code: reminders it wants to send
alt a reminder is allowed
Code->>Code: send it
else the rule does not allow it
Code->>Code: drop it
end
- Writing a program that calls tools is code mode.
- The program runs in a sandbox.
- The check before sending is a guard.
- Cost is counted in tokens.
uv run lab code-mode naive
uv run lab code-modeAdd --case 2 or --case 3 to try the other cases.
There are three sets of bills. Code knows who should get a reminder and in which tone. Both versions pass through the same guard, so a miss is the only way to be wrong. A wrong set adds a penalty of 10000 to the tokens used. Lower is better.
In the saved run the first try got one set wrong, so its total holds one penalty.
| Version | Tokens plus penalties |
|---|---|
| One call per step | 49256 |
| One program | 1766 |
Now break it. Open the pattern file and find allowed_sends. Make it return every send it was given. Then run the test:
uv run pytest tests/promises/test_code_mode.pyThe test fails, because reminders the rule forbids now go out.
- A script error means the program guessed a field name. Give the exact shapes.
- A wrong rule in the program cannot send. It can still miss a bill that is due.
You cannot see each step as it happens. Keep actions that change things out of the sandbox. Let code check them after, as this lab does.
This rule is short enough for code alone. Use code mode when the steps need the model.
Go to Use a computer by its screen. It works a program with no tools of its own.