Highlights

Contents

Part IX
09

Generating useful demonstrations

A program can expand a known expert policy. It can also multiply errors in that policy. Generate semantic tasks and verified state transitions before varying their wording.

THE AGENT AND ITS TRAINING PATHDefine tools01Run the loop02Measure tasks03Choose training04Build LoRA05Count adapters06Serialize traces07Train and validate08Generate tasks09Check traces10Compare models11Prepare inference12

#Section 51Why synthetic traces

A person can write a few stockroom demonstrations carefully. Writing every item, quantity, refusal, wording, and error by hand gets repetitive, and a program can generate the combinations if it knows what correct behavior is.

Limited human demonstrations and scalable data generation

A synthetic trace is a demonstration produced by a specified program, model, or simulator rather than recorded from a person doing the task. Generation scales coverage while keeping provenance; many rows are not new evidence of correctness.

Controlled examples

Controlled examples vary named factors while the contract stays fixed. Our generator varies the quantity and derives remaining stock from the action, so a reservation of 3 from 12 always returns 9 instead of an invented plausible number.

VARY THE TASK VALUES, NOT ONLY THE WORDING4 itemsboltnutwasherscrew3 quantities1355 phrasingscheckreserveif enoughpleasefor job4 x 3 x 5 = 60 parameterized requestscombinations provide coverage, not independent evidence
Figure 5.46The illustrative generator combines four item names, three quantities, and five phrasings into 60 requests. More phrasings do not make one task family independent.

Schick and colleagues’ Toolformer had a language model propose API calls and kept those that lowered prediction loss. Our scripted expert instead knows the correct policy directly. Toolformer paper.

Synthetic data magnifies the teacher’s mistakes. Before scaling, read complete examples, compare simulator and real tool behavior, and test held-out cases. A reproducible generator at least makes its failures reproducible.

#Section 52Tool-use trajectories

A JSON call alone does not say whether the agent was answering a definition, checking stock, or reserving. The trajectory around it gives the action its reason.

User request, tool call, tool result, and additional steps

A trajectory is the ordered sequence of decisions and observations in a task: a request, calls and results, and further steps that depend on earlier evidence. The environment’s starting snapshot is part of its provenance even when the model never sees it.

Final response

The final response closes the task according to what was observed: a reservation of 3 and 9 left on the success path, the reason nothing committed on the unavailable path. A success answer pasted into a failure path is a wrong demonstration, however well formatted.

The seven-message trajectory’s two calls are not interchangeable: the read supplies the reservation’s condition, and the receipt supplies the final count. Generators must keep these dependencies, not just sort messages by role.

Build a structured event record before applying a chat template, so calls can be replayed, call IDs checked, and state recomputed. Serialization then renders a checked trajectory instead of reconstructing a missing environment from prose.

#Section 53Scripted experts

The generator needs a teacher that knows what should happen next. For a simple inventory contract, a short deterministic policy is easier to audit than free model output.

Deterministic correct policy, known tool choice, and known arguments

A scripted expert is a program that follows a known policy for a task family. It reads stock, compares the request with availability, submits the authorized operation when allowed, and reports the receipt. Tool choice and arguments come from the task specification, not guessed text.

THE EXPERT USES THE ENVIRONMENT TO CHOOSE A CORRECT PATHavailable >= requested?yesnoreserve using the task valuesreport insufficient stock12 >= 3; remaining = 912 < 13; no committhe teacher can be deterministic; the task need not be trivial
Figure 5.47The illustrative expert reserves 3 from availability 12 and refuses a request for 13 under the declared policy. Both branches are correct demonstrations.

Generating demonstrations

On the success path, 12 >= 3 permits the action and 12 - 3 = 9 gives the output. On the unavailable path, 12 < 13 blocks the commit and availability stays 12. If the application includes ambiguous requests, expired permissions, timeouts, or retries, the policy needs rules for those too.

The expert is correct only relative to its contract. If the real tool checks stock at commit time and the simulator checks a stale read, the teacher has dropped concurrency semantics. Keep the policy small enough to audit and test it against the real tool before trusting its traces.

#Section 54Task generation

Sixty wordings all ask for 3 bolts at the same stock level, and a model can memorize that answer. The decision has to vary, not just the sentence.

Templates and parameterized tasks

A task template has slots; a parameterized task fills them with values and environment conditions. Vary the required decision, not only its phrasing.

Multi-tool tasks and edge cases

Multi-tool tasks need a read before a write, several reads before a choice, or a write followed by verification. Edge cases probe the contract’s boundaries: exactly enough stock, zero or negative quantities, unknown items, duplicate keys, unavailable tools.

At availability 12, quantity 12 is a boundary that leaves zero after a valid commit, 13 crosses the condition and must not commit, and zero falls outside the positive-integer contract entirely. Treating them as ordinary examples erases the boundary the model must learn.

WITH 12 AVAILABLE, FOUR REQUESTED QUANTITIES TEACH FOUR DIFFERENT DECISIONS01234567891011121314valid and available: 1 to 120: outside contractnot a reservation3: commitsleaves 912: commitsleaves 013: refused12 stays 12
Figure 5.48Computed outcomes for four requested quantities against availability 12. The shaded span is the valid, satisfiable range; each marked case is a distinct demonstration.

Generate semantic task identities first, then phrasings, and fix the held-out combinations before writing any text. Otherwise paraphrases of training tasks leak into validation and inflate generalization.

#Section 55Generating tool calls

The expert knows the quantity, but building JSON by string concatenation breaks when an item name contains a quote. Correct policy and correct serialization are separate jobs.

Correct tool, correct arguments, and schema-valid output

Call generation turns the expert’s decision into a structured request: pick a permitted tool, derive arguments from task and observations, encode with a data serializer, and validate against the runtime’s schema and envelope.

python
call = {
    "id": call_id,
    "name": "reserve",
    "arguments": {
        "sku": task["sku"],
        "quantity": task["quantity"],
        "request_key": task["request_key"],
    },
}
payload = json.dumps(call, allow_nan=False)

This sketch assumes task already passed semantic checks, call_id is unique in its conversation, and json is Python’s module. allow_nan=False blocks nonfinite numbers, but schema validation is still required.

Omitting the operation key makes the trace invalid before execution even when item and quantity are right. Derive keys reproducibly per logical request and keep them across retries so the teacher demonstrates the intended identity semantics.