Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-9.1, LO-9.2, LO-9.3
Prerequisites: the Module 9 reading and Lab 8
Time: about 2.5 hours

In this lab you implement four workflow patterns, in which your code decides each step: routing, parallel calls, an orchestrator with workers, and an evaluator with an optimizer. Then you build an agent, in which the model decides, with limits on its turns and its cost, and trace its run from its log.

No cell sends anything over the network. Each model is a ScriptedModel from promptlab, which answers from a script written in the cell, so its replies are illustrative, not model output. The helper ask(model, text, system) sends one user message and returns the reply’s text; it raises ValueError if the reply didn’t end with "end_turn". The agent can call only the three read-only tools from Lab 8, which read a fictional menu.

Each exercise ends with a check cell that prints passed or fails with a message. Run the cells in order, from the top. Some cells fail on purpose until you finish the exercise above them.

Setup

Trace an independent branch

This branch doesn’t need any other branch’s answer. Predict what ask returns, and the job name you would store it under, then run the cell.

Exercise L9.1 (LO-9.1, ★★, about 25 minutes)

Route to a known worker. Write routed(router, workers, ticket). Get the route from the router model with ask, then send the ticket to the worker in workers that the route names, and return that worker’s reply. Raise ValueError for a route that names no worker, before calling any worker.

Exercise L9.2 (LO-9.1, ★★, about 25 minutes)

Run parallel reads. Write parallel(workers, ticket). Use a ThreadPoolExecutor with two workers. Submit one ask for each stand-in in workers, all with the same ticket, and return a dictionary from job names to replies. Submit every job before you wait for any result, so the jobs can overlap. The check’s second test makes each job wait for the other, so it passes only if both run at the same time.

Exercise L9.3 (LO-9.1, ★★, about 25 minutes)

Check an orchestration plan. Write orchestrate(planner, workers, ticket). The planner’s reply should be a JSON array of unique job names, each the name of a worker. Reject anything else, including an unknown job, with ValueError, before running any worker. Then pass the whole ticket to each worker in the plan, and return the replies keyed by job, in plan order.

Exercise L9.4 (LO-9.1, ★★, about 25 minutes)

Revise with a bounded evaluator. Write optimize(draft, judge, ticket, max_drafts). Call draft with the ticket, followed by the judge’s last review, if there is one. Call judge with the ticket and the new draft. Return the first draft whose review is exactly pass, or None after max_drafts attempts.

Exercise L9.5 (LO-9.2, LO-9.3, ★★, about 25 minutes)

Trace both budget stops. Write agent(model, task, max_turns, budget, reserve), a tool loop like Lab 8’s that uses TOOLS and run_tool, with two limits:

  • Before each call, reserve reserve output tokens, the most a call may use; a real loop would send it as max_tokens. If the credits spent so far plus the reserve would exceed budget, stop with "cost_limit" without calling.

  • After each call, charge the response’s actual usage["output_tokens"]. Raise ValueError if a scripted response used more than the reserve.

  • After max_turns calls without a final answer, stop with "turn_limit".

Return a triple: the status (the stop reason, "cost_limit", or "turn_limit"), the final text or None, and a log. Each log entry is a dictionary with turn (starting at 1), used, spent, stop, and tools, the names of the tools called that turn.

In your notes, compare this loop with one direct lookup in MENU for a pricing question, and say which you would ship and why.

Summary

  • A router picks one known branch, and fails loudly on a route that names none.

  • Parallel calls overlap only if every job is submitted before any result is awaited. They cost the same as running the jobs one after another.

  • An orchestrator’s plan is checked whole before any worker runs, and an evaluator loop stops at its draft limit even when no draft passes.

  • An agent reserves a call’s maximum cost before calling, charges what it used after, and stops at its cost limit or its turn limit. Its log shows what it did, not whether it was right.