Level 02 · Foundations
Working with LLM APIs (Orchestration)
This level turns a single chat reply into a workflow you can trust: ordered messages, streamed text, a typed shape, tools your code actually runs, and several steps with your own if-statements between them. You will also cap how many calls run at once, retry only the failures that are worth retrying, and write down tokens, wait, and cost for every step. An AI engineer needs that loop before a feature can talk to a model without losing the thread, crashing on bad output, or hiding a surprise bill.
Assumes You can explain that a chat model continues from the text you send, one small piece at a time, and that a new chat forgets earlier chats unless you paste them again. · 19 study sessions · 11 modules
Ship it
Capstone: streaming tool-using assistant
Build a small assistant on your own computer that remembers the conversation, prints tokens as they arrive, and can run two or three tools you write in ordinary Python. Where your code reads a model result, require a shape and reject values that do not match. Record timeouts, a small number of retries, and a trace of token counts and a made-up cost.
Use a fake local model so you do not need an account or a secret key. The fake model should still speak the same protocol as a real one: messages, tool calls, and a stream of text slices. Do not add document search, and do not let the model pick unlimited next steps. You choose the loop.
1. A streaming assistant with real tools
1. Create assistant.py with a list called history and a system string that is sent on every turn.
2. Add a fake_model function that yields three text slices for a normal reply, such as "Order", " A17", " is shipped".
3. Print each slice immediately, then store the joined text as an assistant message in history.
4. Register two tools in a dict: one returns a canned order status, and one returns a canned temperature for a city you type.
5. Run a second user turn that relies on the first answer still being in history.
6. Run it with python assistant.py, or py assistant.py on Windows if python is not found. You are done when the second turn's printed context includes the first answer.2. Sequential or parallel tool use
1. Teach fake_model to return two tool calls in one turn when the user text mentions both an order id and a city.
2. Validate each argument with a small function: order id must be a non-empty string, city must be a non-empty string.
3. If validation fails, append a tool message whose content is an error string, and do not run the real function.
4. If both succeed, append both tool results, then have fake_model yield one final sentence that mentions both facts.
5. Run it with python assistant.py, or py assistant.py on Windows if python is not found.
6. You are done when one prompt prints two tool results and a final sentence, and a prompt with a blank id prints an error instead of a crash.3. Structured outputs, retries & timeouts
1. For the final sentence your code will parse, require a tiny schema: a dict with keys reply (string) and ok (true or false).
2. If the fake model emits a non-JSON string, reject it once, then allow a single repair string that is valid JSON.
3. Add a timeout value of 10 seconds on the model call and on each tool. In the fake version, pass the value into the function and print it.
4. Retry only status 429 and 503, at most twice, with a short growing sleep. Do not retry status 401.
5. Run it with python assistant.py, or py assistant.py on Windows if python is not found.
6. You are done when a forced 503 then success prints two attempts, a 401 prints no retry, and a bad JSON row is either repaired or rejected without a traceback.4. Recording usage, latency & cost
1. Add a trace_id string created once per user turn.
2. On every model call and tool call, append a JSON line with that id, the name, token counts you invent from string lengths, latency, retry count, and a cost computed from a tiny price table.
3. Add a command, or a function you call at the end, that prints the lines in order.
4. Run a turn that uses one tool.
5. Run it with python assistant.py, or py assistant.py on Windows if python is not found.
6. You are done when the ordered print shows the model span and the tool span under the same trace id, and you can point at the line with the higher cost.5. Building on LangChain, explaining the raw calls
1. Split the script into three plain stages with names a reader could map to a chain: build_messages, call_model, parse_output.
2. In a comment block at the top, map each stage to the provider request it stands in for: the messages list, the tool schema, and the stream slices.
3. Keep using the fake model. Do not add a paid client.
4. Run one full turn and save the printed stream.
5. In the same comment, retell that turn as four raw steps: send messages, receive tool call, send tool result, receive final slices.
6. You are done when a reader can follow the comment without opening another file and the program still prints the final sentence.6. LLM + software + tools + reliability
1. Read the script and mark four pieces in a note: a model call, an if statement you wrote, a tool, and a failure path.
2. Confirm there is no vector index and no loop that lets the model pick unlimited next actions.
3. Add a scripted conversation: the user asks for an order id that your tool rejects as unknown.
4. The tool returns an error message. The fake model then yields a final sentence that tells the user the order was not found.
5. Run it with python assistant.py, or py assistant.py on Windows if python is not found.
6. You are done when the process exits normally, the error was a tool message rather than a crash, and the last line is a sentence to the user.