Highlights

Contents

Part II
02

Execution, memory, and stopping

Errors can occur before or after a write. Those cases need different recovery. Keep durable action state, preserve decision evidence, and distinguish successful completion from a budget stop.

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

#Section 8Malformed tool calls

The assistant calls reserve_stock, but the registry exposes reserve. The intent looks obvious, yet the name is not an allowed operation, and silently translating it hides a model failure.

Hallucinated tools, wrong argument types, and missing fields

A hallucinated tool is a generated name absent from the permitted registry. Wrong argument types break the schema, such as a list where sku needs a string. Missing fields break presence rules, such as omitting the operation key. Each should produce a precise error before execution.

Invalid JSON, recovery, and retries

Recovery gives the model enough evidence to correct itself. For invalid JSON, the runtime returns a parse error in a protocol-supported observation or asks for regeneration, without pretending a tool ran. For a schema failure it names the field and constraint. A retry repeats an attempt under an explicit policy and budget.

Say the model emits quantity="3". The runtime rejects the string and reports that quantity must be an integer; the corrected quantity=3 passes. That is two attempts and one executed call, and evaluation must keep both or a poor model will look accurate.

WHERE EACH FAILURE STOPS DECIDES HOW TO RECOVERmodelparservalidatorserviceresponseinvalid JSONnever reached the service: regeneratequantity="3"rejected by schema: report the field, retrytimeout on reservemay have committed: reconcile with key r1a lost receipt is not a failed write
Figure 5.10Three illustrative failures stop at different stages. Only the timeout reached the service, so only it can have changed stock, and only it needs reconciliation with the original key.

A syntax error never reached the service. A timed-out write may have committed, and changing request_key during recovery can turn one intended reservation into several. Error handling needs the execution status, not a generic “try again.”

#Section 9Tool execution

The runtime receives a valid reservation. Nothing should let a generated name become a shell command or an arbitrary Python attribute; execution crosses a narrow, checked interface.

Tool registry, dispatch, and passing arguments

A tool registry maps allowed names to trusted implementations. Dispatch picks the implementation after name, schema, and permission checks, and passes the parsed fields against the expected signature. Here the allowed set is exactly get_stock and reserve; a third name has no path to code.

The service must enforce the reservation condition atomically. Reading 12 available and later reserving 3 does not mean 12 are still available, because another user can reserve in between. The commit checks stock and updates the count in one protected operation.

Handling failures, exceptions, and timeouts

An exception is a program’s failure signal. A timeout means the caller stopped waiting before a result arrived. The runtime turns expected failures into a documented error object, records whether a write’s outcome is known, and never leaks credentials or server internals into model context.

A LOST RESPONSE DOES NOT MEAN A LOST WRITEreserve(key = r1)request reaches serverCommit succeeds12 - 3 = 9Response lostclient sees a timeoutRetry same keyask for r1 againStored receiptno second reservationReturn resultavailable stays 9retry the operation identity, not a new action
Figure 5.11The illustrative server commits a reservation before its response is lost. Retrying with the same operation key returns the stored receipt and leaves availability at 9.

Idempotency means repeating an operation with the same identity has the same business effect as doing it once. If the first reserve moved availability from 12 to 9, a retry with r1 returns that same outcome; a new key is a new operation and could drop it to 6. The server must reject the same key with different arguments.

Abandoning a wait does not stop a synchronous in-process function. A hard limit needs cancellation support or process isolation, and the runtime should record the deadline separately from proof that execution stopped.

#Section 10Tool results

The stock tool returns every reservation in the warehouse when the next decision needs one number. Pasting the whole response into the conversation slows the task and buries the answer.

Result formatting and tool-result messages

A tool result is the observation tied to a call. Format it with status, data, and error kept separate, and send it as a tool-result message carrying the call ID. The next assistant message can then explain what happened or act again.

For reserve, a useful success payload has committed=true, a receipt ID, quantity=3, and remaining=9. “Success” alone cannot support a numerical answer, and “remaining=9” without commit status is ambiguous.

Structured outputs, large outputs, and truncation

Structured outputs expose named, typed values. Large outputs need a deliberate reduction: select rows, paginate, or return a handle for a follow-up read. Truncation cuts a payload at a limit and must say what it dropped; cutting raw JSON midway turns a valid observation into invalid syntax.

Trim service output only if the retained result still supports the next decision and final report.

Context cost

Every retained result costs tokens and cached states. A retrieved document may also contain instructions from an untrusted author: keep it as tool data, and keep authorization outside its control. Text asking to change tool permissions is not a permission change.

#Section 11Multi-turn state

The assistant reads stock 12, then forgets it because the next call contains only the user’s request. It must ask again or guess. Memory lives in the input and the program around the model.

Conversation history, assistant messages, tool calls, and tool results

Conversation history is the ordered record of messages sent to the model: the request, assistant decisions, tool calls, and results. It should keep identities like receipt IDs even when the displayed chat hides protocol details.

Our stockroom trace has seven messages including the system message. The first call is not a final answer, and the tool result is not user input. Mixing roles changes the evidence the model sees and teaches the wrong continuation in supervised training.

Stateless model vs stateful harness

A stateless model invocation computes output from its inputs without owning application history. A stateful runtime keeps the record, tool availability, step count, and operation identities across invocations. Server sessions or caches belong to the serving contract, not to the weights.

The inventory database and the conversation record are separate states. Clearing the conversation does not undo a reservation, and losing the KV cache does not touch the database. That tells you what must survive a restart and what can be recomputed.

Keep an audit record of arguments and results for state-changing calls. The generation context can shrink later while the action record stays complete; Section 12 never treats a summary as a receipt.

#Section 12Context management

A long debugging session eventually fills the input limit. Adding a result then fails or pushes out information the assistant still needs, so the runtime must decide what the next decision depends on.

Context-window growth and tool-result growth

The context window bounds the input and generated continuation. Tool descriptions, calls, and results all occupy it; long observations can crowd out the evidence needed for later work.

APPENDED CALLS AND RESULTS CONSUME THE SAME CONTEXTstart320turn 1560turn 2800turn 31,040each turn adds 48 call tokens + 192 result tokens = 240
Figure 5.12Illustrative token budgets grow arithmetically. These values describe a fixture, not the actual tokenization of the displayed messages.

Dropping irrelevant history, summarization, and compression

Summarization replaces verbose history with a shorter statement of what mattered. Compression is the broader goal of keeping relevant information in fewer tokens, including extracting structured facts. Dropping repeated read outputs is fine as long as the active task, current evidence, and operation keys survive.

Summaries must preserve completed writes and unresolved outcomes. Losing “reservation r1 committed” can cause a duplicate action on retry.

SUMMARIZING OLD TURNS: 1,040 TOKENS BECOME 640beforeprompt 320turn 240turn 240turn 2401,040afterprompt 320sum 80latest 240640the 80-token summary must still say "reservation r1 committed"illustrative token counts from the fixture, not tokenizer output
Figure 5.13Computed fixture totals before and after summarizing two old turns. The orange summary replaces 480 tokens of turns with 80.

Sliding context

Sliding context keeps a moving suffix of the record. It is simple and can drop a needed instruction or open dependency, so pin task rules and critical action facts outside the suffix. This is a change to the input record, unlike sliding-window attention, which changes which positions attend to which keys.

#Section 13KV cache across agent turns

After a tool call, the history starts with tokens the model already processed. Recomputing them wastes work, and reuse is possible when the old sequence is an exact prefix of the new one.

Prefix reuse and cache continuation

The KV cache retains processed keys and values. Prefix reuse skips an unchanged initial sequence; cache continuation appends the new suffix. Reuse requires identical computation dependencies.

REUSE REQUIRES AN IDENTICAL TOKEN PREFIXcached prefix560 tokensnew suffix240 tokens800 tokens total; 560 / 800 = 70% reusableediting a past message invalidates its dependent suffix
Figure 5.14The illustrative old prefix and appended suffix need different work. Token identities, positions, weights, and attention configuration must still match.

Cache invalidation and modified history

Cache invalidation discards states whose assumptions changed. Editing an old message changes that token and everything after it. Summaries, a new system prompt, a switched adapter, or a changed chat template can all break reuse, so compare token IDs, not displayed text.

Tool calls add a trap: the raw generated message and its reserialized form can differ. A runtime that parses and rebuilds calls must find the true common token prefix before reusing the cache. The Transformers cache documentation covers strategies and storage trade-offs, but cross-turn validity depends on your inputs. Cache strategies.

Latency implications

Reuse saves prefix computation while retaining its cache memory. It does not eliminate tool latency or generation of the new suffix.

#Section 14Agent stopping conditions

The model keeps reading stock after it has the answer. Every call is legal, and the task makes no progress. Termination must be something the model cannot postpone forever.

EOS and final-answer detection

An end-of-sequence token, or EOS, ends a generated sequence under the model’s protocol. It does not prove the task is done: a tool-call message ends before the tool runs, and a token budget can cut a message short. Final-answer detection recognizes the protocol’s terminal response and records what the system claims it completed.

Maximum steps, maximum tool calls, and repetition detection

A step is one model decision. A decision may hold several calls, so step and call limits differ. Unused budget is no reason to perform another action.

Repetition detection finds cycles, like identical reads with no new evidence. Compare normalized names and arguments and whether the environment changed: the same read can be useful after a write and wasteful before one.

COMPLETION AND BUDGET EXHAUSTION ARE DIFFERENT OUTCOMESModel returnscall, final, or incompleteRuntime checkssteps, calls, time, repeatsTerminal recordsuccess or explicit failurea step limit stops the process, not the unfinished task
Figure 5.15A terminal record distinguishes an observed completion from a forced stop. The budget is illustrative and belongs to the runtime.

Infinite-loop protection

Infinite-loop protection combines call and step ceilings with time, token, and output-size limits. Count invalid attempts too, or a model can loop on malformed JSON forever. Report why the system stopped and what remains unfinished; returning “Done” after the last allowed step disguises failure as success.