Skip to content

< all projects28 steps · easy · Rebuild

An agent from scratch, step by step

You will have built a working tool-using agent with the standard library, one small function at a time: five functions that read a reply, four that survive a busy API, five that describe and run tools, four that make the loop, four that meter it, and six that read a run back and grade it. Each step is a few lines with its own tests, and the earlier functions are provided to the later ones, so the pieces compose without you re-implementing anything. The parts write six files; the ending assembles them and runs the agent over 20 scripted tasks. This is the same system as *An agent from scratch*, cut finer: that project asks for eight modules framed as tickets, this one asks for twenty-eight functions framed as questions.

Start: Extract the text blocksSign in to run steps and keep your progress.
  1. Part 01 · 5 steps

    Replies

    Read what a model sent back.

    1. 01Extract the text blocksnext
    2. 02Join the text
    3. 03Extract the tool calls
    4. 04Did the model stop for a tool?
    5. 05Turn a reply into an assistant message
  2. Part 02 · 4 steps

    Retries

    Survive a busy API.

    1. 06Which errors are worth retrying
    2. 07Exponential backoff
    3. 08Read a Retry-After header
    4. 09Retry with backoff
  3. Part 03 · 5 steps

    Tools

    Describe a tool, and run the one the model asks for.

    1. 10Describe one parameter
    2. 11List the required parameters
    3. 12Build the tool spec
    4. 13Build a tool result
    5. 14Run the tool the model asked for
  4. Part 04 · 4 steps

    The loop

    Call, act, observe, repeat.

    1. 15The first message
    2. 16Send the results back
    3. 17Handle a tool turn
    4. 18Close the loop
  5. Part 05 · 4 steps

    The budget

    Count calls and tokens, and price them.

    1. 19Price a call in micro-dollars
    2. 20Count calls and tokens
    3. 21Is the budget exceeded?
    4. 22Stop the run
  6. Part 06 · 6 steps

    The trace and the grade

    Read a run back and score it.

    1. 23Parse one trace line
    2. 24Per-turn stats
    3. 25The first bad turn
    4. 26Total calls
    5. 27Grade one run
    6. 28Pass rate