Skip to content

Latest commit

 

History

History
96 lines (67 loc) · 2.89 KB

code-mode.md

File metadata and controls

96 lines (67 loc) · 2.89 KB

Write code that calls tools

The problem

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.

How it works

Write code that calls tools.

The model writes one program that calls the tools. This is code mode.

  1. The model gets the tool names and writes a program.
  2. The sandbox runs it. A reminder is only written down there, not sent.
  3. A guard holds the whole rule: age, amount, disputes, holds, tone and repeats.
  4. Only reminders that pass are sent.

For example, the program loops over every bill without another model call.

The flow

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
Loading

Concepts

  • 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.

Run it

uv run lab code-mode naive
uv run lab code-mode

Add --case 2 or --case 3 to try the other cases.

Prove it

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.py

The test fails, because reminders the rule forbids now go out.

Stuck points

  • 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.

What to watch for

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.

Where to go next

Go to Use a computer by its screen. It works a program with no tools of its own.