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.

These exercises practice retrieving what the Module 9 reading explained. They aren’t graded. Work each one on paper first, then check your answer, and only then confirm it with code from Lab 9 if you like. Budgets are in Lab 9’s credits, where one credit is one output token. The answers are in the collapsed blocks; open one only after you have an answer of your own.

Worked example: checking a plan before it runs

The workers are parts and labor. For each planner reply, how many workers run?

  1. ["parts", "labor"] is a list of two different strings, and each names a worker. The plan is valid, so both workers run, in that order: 2.

  2. {"jobs": ["parts"]} is valid JSON, but it is an object, not a list. The code rejects the whole reply before starting anything: 0.

  3. ["labor", "parts", "paint"] names a worker that doesn’t exist. Checking item by item while running would start labor and parts before failing on paint. Checking the whole plan first rejects it with no worker started: 0.

Exercise P9.1 (LO-9.1, ★, about 10 minutes)

The planner asks for ["labor", "parts", "labor"]. The workers’ contracts forbid duplicate jobs. How many workers run?

Hint 1

Decide whether the plan is valid before you count anything.

Hint 2

Validate the full plan first, as in the worked example’s third reply.

Answer

Zero. labor appears twice, so the plan breaks the contract, and the code rejects it before starting any worker. The common mistake is to answer 2, by running the unique jobs, or 3, by running the plan as written. Either way, the code silently repairs or ignores a plan that the planner got wrong, and a duplicate may mean the planner misread the ticket.

Worked example: a budget trace

A budget of 5 credits, a reserve of 3, and a turn limit of 4:

TurnCheck before the callCall?UsedSpent after
10 + 3 = 3, not over 5Yes22
22 + 3 = 5, not over 5Yes24
34 + 3 = 7, over 5No: cost_limit4

A spend of exactly the budget is allowed, so turn 2 runs. One credit remains after turn 2, and a third call would need a new budget.

Exercise P9.2 (LO-9.2, ★★, about 15 minutes)

A budget is 8, the reserve is 4, and the turn limit is 4. Two tool calls in a row spend 3 credits each. Why does the run stop?

Hint 1

Write the trace as a table, as in the worked example.

Hint 2

Before the third call, compare what has been spent plus the next reservation with the budget.

Answer
TurnCheck before the callCall?UsedSpent after
10 + 4 = 4, not over 8Yes33
23 + 4 = 7, not over 8Yes36
36 + 4 = 10, over 8No: cost_limit6

The run stops after two calls, with cost_limit. Only 2 credits remain, and the next call could use up to 4. The turn limit of 4 never comes into play. A common mistake is to compare the next call’s likely use, 3, with the 2 that remain, or to compare 6 with 8 and keep going. The check uses the reserve, because the loop can’t know in advance what a call will use.

Worked example: choosing a design

A customer asks, “Are you open on Friday?” The answer is one fixed lookup: the hours for Friday, 09:00–15:00. No step depends on another, and nothing needs a model to decide what to do next. Code that reads the hours, or one call with the shop_hours tool, is enough. An agent loop would add calls and ways to fail, and buy nothing.

Exercise P9.3 (LO-9.3, ★★, about 15 minutes)

Compare routing a ticket to one of two known queues with deciding which service reads a mixed request needs.

Hint 1

For each task, ask who should choose the next step.

Hint 2

Ask whether the steps are known before the request arrives.

Answer
  • Routing to two known queues: use fixed routing, one classifying call followed by the branch. The task is a single decision, the steps are known in advance, and an error is cheap to measure with labeled cases (Module 7) and to correct.

  • Reads for a mixed request: the reads depend on what the ticket mentions, so an orchestrator or an agent can help, with its plan or its calls validated and its turns and cost limited. Use it only if an evaluation shows that its answers improve enough to justify the extra calls.

A common mistake is to choose an agent for both, because an agent can do both. The reading’s rule is the simplest design that meets the need.

Worked example: counting an evaluator’s calls

A loop allows three drafts. The judge rejects draft 1 and passes draft 2. Each attempt makes two calls, a draft and a judgment, so the loop made 2 × 2 = 4 calls and returns draft 2. Draft 3 is never written.

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

A judge never passes a draft, and the optimizer allows three drafts. What is the maximum number of stage calls?

Hint 1

Each attempt calls both stages.

Hint 2

The loop stops when the draft limit runs out, whatever the judge says.

Answer

Six: three drafts and three judgments. After the third rejection, the loop returns a failed result, such as None, and the caller falls back, for example to a person. A common mistake is to count 3, forgetting the judge’s calls, or 7, adding a fourth draft. Another is to return the last draft anyway, which the judge rejected.