Skip to content

< all projects27 steps · easy · Rebuild

Build a model client

You will have built a model client with the standard library, one small function at a time: 3 that build the conversation, 5 that read a reply and finish one that was cut off, 5 that get JSON back and ask again when it is wrong, 4 that turn a stream into a reply, 3 that survive a busy API, 3 that count the cost, and 4 that put it all behind one client. 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 writing anything twice. The model is handed to you as an object with one method, `chat`, and is scripted while you build, so a rate limit or a cut-off reply arrives exactly when a step needs one. At work you would reach for the provider's SDK, a validation library and a retry library. This project is what those do for you. When every step has passed, the seven files assemble into one client and you can run it against a real model, with a receipt at the end.

Start: A user messageSign in to run steps and keep your progress.
  1. Part 01 · 3 steps

    Messages

    Build the conversation you send.

    1. 01A user messagenext
    2. 02An assistant message
    3. 03Add a turn to the history
  2. Part 02 · 5 steps

    Replies

    Read what came back, and get the rest when it was cut off.

    1. 04The text of a reply
    2. 05Was the reply cut off?
    3. 06The tokens a reply used
    4. 07Make the call
    5. 08Get the rest of a cut-off reply
  3. Part 03 · 5 steps

    Structured output

    Get JSON back, and ask again when it is wrong.

    1. 09Take off the code fence
    2. 10Find the JSON object
    3. 11Say what is wrong with it
    4. 12Write the correction
    5. 13Ask until the JSON is right
  4. Part 04 · 4 steps

    Streaming

    Turn a stream of events into a reply.

    1. 14The text in one event
    2. 15Join the stream
    3. 16Why the stream ended
    4. 17A reply from a stream
  5. Part 05 · 3 steps

    Retries

    Survive a busy API.

    1. 18Which errors are worth retrying
    2. 19How long to wait
    3. 20Retry with backoff
  6. Part 06 · 3 steps

    Cost

    Know what every call cost.

    1. 21Price one call
    2. 22Keep a ledger
    3. 23Print the receipt
  7. Part 07 · 4 steps

    The client

    Put the pieces behind four functions.

    1. 24Start a client
    2. 25One call, retried and counted
    3. 26Send a message
    4. 27Ask for JSON