Course contents 11 / 27

11 / 27 · Language Models as Agents

Tool use and function calling

How a language model calls functions: tool definitions in JSON Schema, the round trip of a call, parallel calls, constrained decoding that masks logits so every call is valid by construction, Toolformer's self-supervised training signal, code as the action, and errors and retries.

On this page 8

The previous note gave the model room to compute before it answers, but no new information and no way to change anything. Its policy can only write text. The actions of the policy note were strings that a hand-written parser had to recognize, and anything the parser did not recognize was lost. Tool use replaces that parser with a contract. Each tool is declared as a typed function, the model emits a call in a fixed structure, and the runtime checks the call, executes it and returns the result. The tools and agents note of the NLP course gives the protocol in four steps. This note goes into each step: what a function definition contains, the exact messages of a round trip, calls made in parallel, how decoding can be constrained so that every call is valid by construction, how models learned to call tools at all, code as the action, and what the runtime does when a call fails.

Notation. Tools f1,…,fmf_1, \dots, f_m, each with a name, a description and a schema SiS_i for its arguments. A call is a triple (identifier, name, arguments), the arguments a JSON object. VV is the vocabulary, z∈R∣V∣z \in \mathbb R^{|V|} the logits at one decoding step and p=softmax⁡(z)p = \operatorname{softmax}(z); L\mathcal L is the set of valid outputs, and a prefix uu is live if some sequence of tokens extends it to an element of L\mathcal L. M(u)⊆VM(u) \subseteq V is the set of tokens allowed after uu.

Baseline: actions parsed from free text#

The first language-model agents wrote their actions in an ad hoc format, such as search[Dunmore] in ReAct, and a regular expression read them. The format is a convention the model follows only probabilistically. It may write Search: Dunmore, wrap the action in a sentence, put a quote inside an argument without escaping it, or name a tool that does not exist. Each such slip costs a step, and the costs compound over a long task. Worked example. If one action in 50 is malformed, ε=0.02\varepsilon = 0.02, an agent that takes 20 actions produces at least one malformed action with probability

1−(1−ε)20=1−0.9820=0.33.1 - (1 - \varepsilon)^{20} = 1 - 0.98^{20} = 0.33 .

One run in three hits a parse error, even though each single action is well formed 98 % of the time. Arguments add a second problem: a well-formed call can still pass a string where a number is expected, or an item the shop does not sell.

A function definition#

A tool definition has three parts. In the format used by most model providers, a tool of the toy shop of the Code section reads:

{
  "name": "get_price",
  "description": "Price of one item in the shop, in euros.",
  "parameters": {
    "type": "object",
    "properties": {
      "item": { "type": "string", "enum": ["lamp", "chair", "desk"] }
    },
    "required": ["item"],
    "additionalProperties": false
  }
}
  • The name is what the model writes to call the tool. It should say what the tool does, because the model chooses tools by reading.
  • The description is a prompt. It is the only documentation the model sees, so it states what the tool returns, its units, when to use it and when not to.
  • The parameters are a JSON Schema (JSON Schema, 2020): the type of each argument, which arguments are required, and constraints such as enum (a fixed list of values), minimum and maximum, pattern (a regular expression a string must match) and additionalProperties: false (no other keys). The schema serves twice: the chat template renders it into the context so the model knows how to call, and the runtime uses it to check the call.

Providers wrap the same content in slightly different envelopes: parameters in OpenAI’s API, input_schema in Anthropic’s, inputSchema in the Model Context Protocol. The definitions are part of the context of every call the agent makes, so their tokens are paid at every step; the two definitions of the Code section take 530 characters, roughly 130 tokens at about four characters per token, and real agents with dozens of tools spend thousands.

The round trip of a tool call#

A model that supports tools has been trained on dialogues containing tool definitions, calls and results, in a format with special tokens around calls (Instruction tuning and alignment). When it decides to call, it writes a call block instead of text, and the serving code returns it to the program as structured data rather than as a message for the user. The program, not the model, does the rest.

Figure

The round trip of a tool call: model, runtime, tools

The round trip of a tool call: model, runtime, toolsA sequence diagram with three lanes: the model, the runtime and the tools; time runs downwards. The runtime passes the definitions and the question to the model. The model replies with three get_price calls: c1 lamps, c2 chair, c3 desk. Validation rejects c1. c2 and c3 run in parallel and return 41.0 and 129.0. The model receives an error for c1 and the two prices, calls c4 get_price(lamp) and c5 calculator, receives 24.5 and 284.5, and answers 284.5 euros.modelruntimetoolstool definitions + question1three get_price callsc1 lamps · c2 chair · c3 desk2validatec1 ✗ not in enum3c2, c3 in parallel41.0 · 129.0 after 0.8 s4c1 error: use lamp, chair or deskc2 41.0 · c3 129.05c4 lamp, then c5 calculatorc4, c524.5 · 284.524.5, then 284.5284.5 euros6The round trip of a tool call: model, runtime, toolsA sequence diagram with three lanes: the model, the runtime and the tools; time runs downwards. The runtime passes the definitions and the question to the model. The model replies with three get_price calls: c1 lamps, c2 chair, c3 desk. Validation rejects c1. c2 and c3 run in parallel and return 41.0 and 129.0. The model receives an error for c1 and the two prices, calls c4 get_price(lamp) and c5 calculator, receives 24.5 and 284.5, and answers 284.5 euros.modelruntimetoolsdefinitions+ question1c1 lampsc2 chairc3 desk2validatec1 ✗ enum3c2, c3in parallel41.0, 129.04c1: errorc2, c3: prices5c4 lampc5 calculatorc4, c524.5, 284.524.5, 284.5answer: 284.5 euros6
The model only proposes calls; the runtime checks and executes them. The identifier ties each result to its call, so the order of the results does not matter. The validation error goes back like any other result: the model sees which values are allowed and fixes the call on its next turn. The calculator needs the prices, so it is called later rather than in parallel with them.
  1. Define. The runtime sends the tool definitions and the user’s message: How much do three lamps, two chairs and a desk cost?
  2. Call. The model replies with three calls in one turn, each with an identifier: c1 get_price(lamps), c2 get_price(chair), c3 get_price(desk).
  3. Validate. The runtime checks each call against its schema. c1 fails: lamps is not in the enum.
  4. Execute. The two valid calls run at the same time; the turn waits 0.8 s, the time of the slower one.
  5. Return. Three tool messages go back, each carrying the identifier of its call: an error for c1 that names the allowed values, 41.0 for c2, 129.0 for c3.
  6. Continue. The model reads the error and calls get_price(lamp) as c4, then the calculator with 3 * 24.5 + 2 * 41.0 + 129.0 as c5, and finally answers: Three lamps, two chairs and a desk cost 284.5 euros.

Three properties of the protocol carry the weight.

  • Identifiers pair results with calls. A turn may contain several calls and the results may arrive in any order; the identifier, not the position, tells the model which result answers which call.
  • Errors are observations. The validation error for c1 is sent back like any other result. The model sees why the call failed and fixes it on the next turn; nothing crashes. This is the same move as returning a compiler error to a programmer.
  • The model never executes anything. It proposes calls; the runtime decides whether to run them. Checks, permissions, confirmations for actions with side effects and sandboxes all live in the runtime, where they cannot be talked out of by text in the context. A tool result is data, and text inside it that looks like an instruction is still data (Agent security and prompt injection).

Parallel calls. When several calls do not depend on each other, the model can emit them in one turn and the runtime can run them concurrently. In the example, the first turn’s two valid calls take 0.8 s each: in parallel the turn takes 0.8 s, one after another 1.6 s, and the whole task 1.7 s against 2.5 s. The saving in model turns matters as much: one call per turn would need six turns of the model instead of four, and every turn re-reads the whole context. Only independent calls can be parallel: the calculator needs the prices, so it waits for a later turn. LLMCompiler (Kim et al., 2024) pushes this further by having the model write a plan of calls with their dependencies first, then dispatching every call as soon as its inputs are ready; on its benchmarks it reported up to 3.7 times lower latency and up to 6.7 times lower cost than a ReAct loop that calls tools one at a time.

Constrained decoding: valid by construction#

Validation catches an invalid call after it was generated. Constrained decoding prevents it. The output has to belong to a set L\mathcal L of valid strings, given by a grammar, a regular expression or a JSON Schema. At every step, the decoder removes the tokens that would make the output impossible to complete, and samples from what remains:

  z~w={zwif w∈M(u),−∞otherwise,p~(w)=p(w) 1[w∈M(u)]∑w′∈M(u)p(w′),M(u)={ w:uw is live }.  \boxed{\;\tilde z_w = \begin{cases} z_w & \text{if } w \in M(u), \\ -\infty & \text{otherwise,} \end{cases} \qquad \tilde p(w) = \frac{p(w)\,\mathbb 1[w \in M(u)]}{\sum_{w' \in M(u)} p(w')}, \qquad M(u) = \{\, w : u w \text{ is live} \,\}.\;}

The end-of-sequence token is allowed only when u∈Lu \in \mathcal L. Why every output is valid: the empty prefix is live; if uu is live, every token in M(u)M(u) keeps it live, and M(u)M(u) is not empty, because some continuation exists; so every prefix the decoder ever produces is live, and it can stop only at a complete element of L\mathcal L. Validity holds whatever the model’s logits are.

Worked example. The Code section constrains a call to the form {"item": "…", "qty": …}, with the item in the shop’s enum and an integer quantity, over a toy vocabulary of 17 tokens. After the prefix {"item": ", the stand-in model prefers the plural:

Figure

The mask on the logits after the prefix {"item": "

The mask on the logits after the prefix {"item": "A table of eight rows: the token, its logit, whether it is allowed, and two probability bars. lamps: logit 2.0, forbidden, 0.437 before masking. lamp: 1.5, allowed, 0.265 before and 0.613 after. Sure: 0.5, forbidden. chair: 0.2, allowed, 0.167 after. de: 0.0, allowed, 0.137 after. desk: −0.5, allowed, 0.083 after. }: −1.0, forbidden. The other 10 tokens together have 0.011 before masking and are forbidden.tokenlampslampSurechairdedesk}10 other tokenslogit zp before masking2.00.4371.50.2650.50.0980.20.0720.00.059−0.50.036−1.00.022−4.00.0111allowed✗✓✗✓✓✓✗✗2p after masking0.0000.6130.0000.1670.1370.0830.0000.000÷ 0.433, the allowed mass3The mask on the logits after the prefix {"item": "A table of eight rows: the token, its logit, whether it is allowed, and two probability bars. lamps: logit 2.0, forbidden, 0.437 before masking. lamp: 1.5, allowed, 0.265 before and 0.613 after. Sure: 0.5, forbidden. chair: 0.2, allowed, 0.167 after. de: 0.0, allowed, 0.137 after. desk: −0.5, allowed, 0.083 after. }: −1.0, forbidden. The other 10 tokens together have 0.011 before masking and are forbidden.tokenlampslampSurechairdedesk}10 otherszbefore2.00.4371.50.2650.50.0980.20.0720.00.059−0.50.036−1.00.022−4.00.0111ok✗✓✗✓✓✓✗✗2after0.0000.6130.0000.1670.1370.0830.0000.000÷ 0.4333
The most probable token, lamps, leads to a dead end: after lamp a quote must follow, not an s. The mask sets its logit to −∞, and its probability 0.437 is shared among the allowed tokens in proportion to their original probabilities. The token de is allowed because desk can be written as de followed by sk; the item desk gets 0.137 + 0.083 = 0.220.

The tokens lamps, Sure and } would make the call impossible to complete and are masked. Four tokens remain. In unnormalized terms their weights are e1.5+e0.2+e0+e−0.5=7.310e^{1.5} + e^{0.2} + e^{0} + e^{-0.5} = 7.310 out of a total of 16.89916.899, so ∑w∈M(u)p(w)=0.433\sum_{w \in M(u)} p(w) = 0.433, and dividing by it gives lamp 0.6130.613, chair 0.1670.167, de 0.1370.137 and desk 0.0830.083. The token de stays because desk can also be written as de followed by sk; the item desk thus keeps probability 0.137+0.083=0.2200.137 + 0.083 = 0.220 through two tokenizations. Over 2,000 samples at τ=1\tau = 1, 15.8 % of the unconstrained outputs parse and pass the schema, and 100 % of the constrained ones.

Three details decide whether this works in practice.

  • Tokens are not characters. A token like ", "qty": spans several symbols of the grammar, so the check runs the token’s whole string through the automaton. And a character-level prefix can be legal yet dead: in the toy vocabulary the single token " after the item is a legal start of ", "qty": , but no token continues it, so the decoder must check liveness over tokens, not over characters. Efficient implementations compile the regular expression or schema into a finite-state machine and precompute, for every state, the set of tokens that lead to live states, so a step costs a lookup (Willard and Louf, 2023). Nested structures, such as JSON objects inside arrays to any depth, need a pushdown automaton or a bound on the depth.
  • Masking token by token is not conditioning on validity. The distribution we would like is the model’s distribution restricted to valid strings, p(y∣y∈L)∝p(y) 1[y∈L]p(y \mid y \in \mathcal L) \propto p(y)\,\mathbb 1[y \in \mathcal L]. Masking renormalizes at each step instead, without looking ahead at how much valid probability each branch leads to. Worked example. The valid strings are ab and cd. The model gives a and c probability 0.50.5 each; after a it puts 0.10.1 on b and the rest on invalid tokens, after c it puts 0.90.9 on d. Conditioning on validity gives P(ab)=0.05/(0.05+0.45)=0.1P(\texttt{ab}) = 0.05 / (0.05 + 0.45) = 0.1 and P(cd)=0.9P(\texttt{cd}) = 0.9. Masking keeps both first tokens at 0.50.5 and then forces the only valid second token, so it returns ab half of the time, a string the model itself considers unlikely. Park et al. (2024) show this distortion in real grammars and propose sampling that corrects it.
  • Valid is not right. The schema guarantees the shape; it does not choose the right tool or the right item. And forcing a format from the first token can cost accuracy: Tam et al. (2024) found that requiring JSON or other strict formats lowered models’ scores on reasoning tasks. A common remedy is to let the model reason freely and constrain only the call. OpenAI’s structured outputs, introduced in August 2024, apply this kind of constrained decoding to a supplied schema; on the company’s own evaluation of complex schemas the constrained model scored 100 %, which is what the construction guarantees.

How models learned to call tools: Toolformer#

A model calls tools well only if its training showed it when a call helps. Toolformer (Schick et al., 2023) produced that training data without human labels, in three steps on a plain-text corpus.

  1. Sample. A prompt with a few examples asks the model to insert candidate calls into ordinary text, such as [Calculator(18 / 24) → before the share in a sentence about a ratio. Positions where the model’s probability of starting a call is high are kept, and several calls are sampled at each.
  2. Execute every call and insert its result.
  3. Filter. Keep a call only if it makes the following text easier to predict. With L+L^+ the model’s weighted loss on the next tokens when the call and its result are inserted before them, and L−L^- the smaller of the losses with no call and with the call but no result,
keep the call ifL−−L+≥τf,\text{keep the call if} \quad L^- - L^+ \ge \tau_f ,

with τf=1\tau_f = 1 for most tools. The comparison with the call-without-result case ensures that the result, not the mere presence of a call, is what helps. Worked example. Before the text … is 2.1 million, the loss of the next tokens is 4.14.1 with no call, 4.04.0 with the call [QA(population of the city)] but no result, and 1.21.2 with its result. Then L−=min⁡(4.1,4.0)=4.0L^- = \min(4.1, 4.0) = 4.0 and L−−L+=2.8≥1L^- - L^+ = 2.8 \ge 1: the call is kept. A call whose result lowers the loss from 3.23.2 to 3.03.0 is dropped.

The model is then fine-tuned on the text with the kept calls in it, and at inference the runtime executes a call whenever the model writes one. With GPT-J (6.7 B parameters) and tools for question answering, a calculator, Wikipedia search, translation and a calendar, zero-shot accuracy on math word problems rose from 7.5 % to 40.4 % on ASDiv, 5.2 % to 29.4 % on SVAMP and 9.9 % to 44.0 % on MAWPS, above GPT-3 175B’s 14.0 %, 10.0 % and 19.8 % in the same evaluation. Tool use helped only from about 775 M parameters up. Current models learn tool calling in supervised fine-tuning on dialogues with calls, and increasingly by reinforcement learning on tasks whose outcome depends on the calls, the subject of Training agents with reinforcement learning.

Code as the action#

A JSON call names one function. A program can call many, combine their results, loop and branch, all in one action. CodeAct (Wang et al., 2024) makes Python code the action space: the model writes a short program, the runtime runs it in an interpreter, and the printed output and any error message become the observation. Across 17 models on API-Bank and on M3ToolEval, a benchmark of 82 tasks that need several tools, code actions gave up to 20 percentage points higher success than JSON or text actions; for gpt-4-1106-preview on M3ToolEval, 74.4 % against 52.4 % with JSON and 53.7 % with text, in 5.5 turns on average instead of 7.6 and 7.7. The NLP course shows a small code agent at work.

The price is that the action can do anything the interpreter can do. JSON calls are checked against a schema and dispatched to functions the designer wrote; a program has to be confined instead, in a container or virtual machine with no network and no credentials it does not need (Virtual machines and containers). Coding agents, which take this to its end, are the subject of Coding agents.

Errors and retries#

Calls fail in four ways, and each has its own place in the runtime.

failureexamplewho handles ithow
malformed outputtext where a call was expected, broken JSONthe decoder or the runtimeconstrained decoding, or return a parse error to the model
invalid argumentslamps not in the enum, a string for a numberthe runtime, then the modelvalidate against the schema; return the error message as the result
execution errora timeout, a server error, an exception in the toolthe runtimeretry transient errors with backoff; return persistent errors to the model
valid but wrongthe right shape, the wrong item or the wrong toolthe model, from later observationsnothing in the protocol catches it; only checking the result does

Transient errors are retried by the runtime, without spending a model turn. If an attempt fails independently with probability 0.30.3, three attempts all fail with probability 0.33=0.0270.3^3 = 0.027, so the call succeeds with probability 0.9730.973. The wait between attempts usually doubles (exponential backoff), so that a service under load is not hit harder. In the flaky run of the Code section, five attempts were needed for four calls, the task took 2.3 s instead of 1.7 s, and the model never saw the failures.

Retries by the model are not independent. Asked again with the same context, the model tends to repeat the same mistake, so a retry helps only if the context changes, which is why the error message goes back into it. A useful error message says what was wrong and what is allowed, as the enum error does.

Only idempotent calls may be retried blindly. Reading a price twice is harmless. Sending a payment or an email twice is not, and a timeout does not tell the runtime whether the first attempt took effect. Calls with side effects need an idempotency key, which lets the receiving service recognize a repeat, or a confirmation step before they run, as in the NLP course.

Tools do not have to be written into the agent’s own program. The Model Context Protocol (MCP), introduced by Anthropic in November 2024, lets separate servers offer tools with the same three fields, a name, a description and an inputSchema, which a client discovers by listing them and invokes by sending calls. Workflows, frameworks and protocols treats it in detail.

What changed#

actions parsed from free textfunction calling
action formata convention, read by a regular expressiona call block with name, identifier and JSON arguments
what the model knows about a toola line in the prompta definition with a description and a schema
invalid actionslost, or guessed by the parserrejected by validation and returned as errors, or ruled out by constrained decoding
several actions per turnnoparallel calls, paired with results by identifier
who executesthe agent programthe runtime, with checks, retries and permissions
how the model learned itprompt examplestraining on calls: Toolformer’s filter, then fine-tuning and RL

Limitations#

  • Schemas check shape, not meaning. A valid call can be the wrong call, and nothing in the protocol notices.
  • Constrained decoding distorts the distribution it samples from, and forcing a format from the first token can hurt reasoning.
  • Definitions cost context. Every tool’s description is re-read at every step, and models choose less reliably among many similar tools; large tool sets need retrieval of tools or several narrower agents.
  • Tool results are untrusted input. A result is text that enters the context, and text can carry instructions.
  • Side effects make retries dangerous. A timeout leaves the state of the world unknown.
  • Code actions need a sandbox. They are strictly more expressive than JSON calls, and strictly harder to confine.

What you should remember#

  • A tool is a name, a description written for the model and a JSON Schema for its arguments. The schema is rendered into the context and used again by the runtime to validate.
  • The round trip: the model emits calls with identifiers; the runtime validates, executes and returns one message per call; errors are returned as results so the model can fix the call.
  • Independent calls can be emitted in one turn and run in parallel: latency becomes the slowest call instead of the sum, and fewer model turns re-read the context.
  • Constrained decoding sets the logits of tokens that would make the output impossible to complete to −∞-\infty and renormalizes. Every output is then valid by induction on live prefixes. The check is over tokens, not characters, and per-step masking is not the same as conditioning on validity.
  • Toolformer kept a sampled call only if its result lowered the loss of the following text by at least τf\tau_f, which turned plain text into training data for tool use.
  • Code as the action composes many calls in one step and needs a sandbox.
  • The runtime retries transient errors with backoff and only for idempotent calls; the model fixes the errors it is shown.

Code#

A tool registry, a validator for the part of JSON Schema in use, a runtime that validates, executes in parallel and retries, a scripted stand-in model that emits calls and reads the results, and a constrained decoder over a toy vocabulary with a liveness check over tokens. With a real model, toy_model becomes a request to a model with the tool definitions, and toy_logits becomes its next-token logits inside the serving engine; the registry, the validator, the runtime and the mask stay as they are. Seeds are fixed; exact numbers can differ slightly across platforms and library versions. The script runs in under a second.

import ast, json, operator, re
from functools import lru_cache
import numpy as np

# 1. a tool registry: a Python function plus a JSON Schema for its arguments
TOOLS = {}
def tool(name, description, parameters, latency):
    def register(fn):
        TOOLS[name] = {'fn': fn, 'latency': latency,
                       'definition': {'name': name, 'description': description, 'parameters': parameters}}
        return fn
    return register

PRICES = {'lamp': 24.5, 'chair': 41.0, 'desk': 129.0}            # a toy shop
@tool('get_price', 'Price of one item in the shop, in euros.',
      {'type': 'object', 'properties': {'item': {'type': 'string', 'enum': list(PRICES)}},
       'required': ['item'], 'additionalProperties': False}, latency=0.8)
def get_price(item):
    return PRICES[item]

OPS = {ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul, ast.Div: operator.truediv}
def arith(node):                                                  # +, -, *, / on numbers, nothing else
    if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)): return node.value
    if isinstance(node, ast.BinOp) and type(node.op) in OPS: return OPS[type(node.op)](arith(node.left), arith(node.right))
    raise ValueError('only numbers and + - * / are allowed')
@tool('calculator', 'Evaluate an arithmetic expression with + - * / and parentheses.',
      {'type': 'object', 'properties': {'expression': {'type': 'string', 'pattern': r'^[0-9+\-*/(). ]+$'}},
       'required': ['expression'], 'additionalProperties': False}, latency=0.1)
def calculator(expression):
    return arith(ast.parse(expression, mode='eval').body)

# 2. a validator for the part of JSON Schema used here
TYPES = {'object': dict, 'string': str, 'integer': int, 'number': (int, float), 'boolean': bool, 'array': list}
def validate(x, schema, path='arguments'):
    if not isinstance(x, TYPES[schema['type']]): return [f'{path} must be of type {schema["type"]}']
    errors = []
    if schema['type'] == 'object':
        props = schema.get('properties', {})
        errors += [f'{path}.{k} is required' for k in schema.get('required', []) if k not in x]
        if schema.get('additionalProperties') is False:
            errors += [f'{path}.{k} is not allowed' for k in x if k not in props]
        for k, v in x.items():
            if k in props: errors += validate(v, props[k], f'{path}.{k}')
    if 'enum' in schema and x not in schema['enum']: errors.append(f'{path} must be one of {schema["enum"]}')
    if 'pattern' in schema and not re.search(schema['pattern'], x): errors.append(f'{path} does not match {schema["pattern"]}')
    return errors

# 3. the runtime: validate, execute, retry transient failures, and return every result as a tool message
def execute(call, rng, flaky=0.0, retries=3):
    t = TOOLS.get(call['name'])
    if t is None: return {'error': f'unknown tool {call["name"]}'}, 0.0, 0
    try: args = json.loads(call['arguments'])
    except json.JSONDecodeError as e: return {'error': f'arguments are not JSON: {e.msg}'}, 0.0, 0
    errors = validate(args, t['definition']['parameters'])
    if errors: return {'error': '; '.join(errors)}, 0.0, 0          # the model sees why and can fix the call
    waited = 0.0
    for attempt in range(1, retries + 1):
        waited += t['latency']
        if rng.random() >= flaky: return {'result': t['fn'](**args)}, waited, attempt
        waited += 0.5 * 2 ** (attempt - 1)                          # transient failure: back off, try again
    return {'error': 'tool unavailable, try later'}, waited, retries

# 4. a scripted stand-in for the model: it reads the messages and emits calls or an answer
def toy_model(messages):
    results = {m['tool_call_id']: json.loads(m['content']) for m in messages if m['role'] == 'tool'}
    if not results:                                                 # three independent calls in one turn
        return {'role': 'assistant', 'tool_calls': [
            {'id': 'c1', 'name': 'get_price', 'arguments': '{"item": "lamps"}'},
            {'id': 'c2', 'name': 'get_price', 'arguments': '{"item": "chair"}'},
            {'id': 'c3', 'name': 'get_price', 'arguments': '{"item": "desk"}'}]}
    if any(r.get('error', '').startswith('tool unavailable') for r in results.values()):
        return {'role': 'assistant', 'content': 'A tool is unavailable right now; please try again later.'}
    if 'error' in results.get('c1', {}) and 'c4' not in results:   # read the error, fix the argument
        return {'role': 'assistant', 'tool_calls': [{'id': 'c4', 'name': 'get_price', 'arguments': '{"item": "lamp"}'}]}
    if 'c5' not in results:
        lamp, chair, desk = (results[k]['result'] for k in ('c4', 'c2', 'c3'))
        return {'role': 'assistant', 'tool_calls': [
            {'id': 'c5', 'name': 'calculator', 'arguments': json.dumps({'expression': f'3 * {lamp} + 2 * {chair} + {desk}'})}]}
    return {'role': 'assistant', 'content': f'Three lamps, two chairs and a desk cost {results["c5"]["result"]} euros.'}

def agent(user, rng, flaky=0.0, max_turns=6):
    messages = [{'role': 'system', 'content': json.dumps([t['definition'] for t in TOOLS.values()])},
                {'role': 'user', 'content': user}]
    clock, sequential, attempts = 0.0, 0.0, 0
    for _ in range(max_turns):
        reply = toy_model(messages)
        messages.append(reply)
        if 'tool_calls' not in reply: return reply['content'], messages, clock, sequential, attempts
        outs = [execute(c, rng, flaky) for c in reply['tool_calls']]
        clock += max(w for _, w, _ in outs)                         # parallel calls: wait for the slowest
        sequential += sum(w for _, w, _ in outs)                    # one after another: wait for all
        attempts += sum(k for _, _, k in outs)
        for c, (out, _, _) in zip(reply['tool_calls'], outs):
            messages.append({'role': 'tool', 'tool_call_id': c['id'], 'content': json.dumps(out)})
    return None, messages, clock, sequential, attempts

answer, messages, clock, sequential, attempts = agent('How much do three lamps, two chairs and a desk cost?', np.random.default_rng(0))
for m in messages[2:]: print(m['role'], m.get('tool_calls') or m.get('content'))
print(answer, round(clock, 2), round(sequential, 2), attempts)
print(len(messages[0]['content']))                                 # characters of tool definitions, re-sent every turn
# assistant [{'id': 'c1', 'name': 'get_price', 'arguments': '{"item": "lamps"}'}, {'id': 'c2', 'name': 'get_price', 'arguments': '{"item": "chair"}'}, {'id': 'c3', 'name': 'get_price', 'arguments': '{"item": "desk"}'}]
# tool {"error": "arguments.item must be one of ['lamp', 'chair', 'desk']"}
# tool {"result": 41.0}
# tool {"result": 129.0}
# assistant [{'id': 'c4', 'name': 'get_price', 'arguments': '{"item": "lamp"}'}]
# tool {"result": 24.5}
# assistant [{'id': 'c5', 'name': 'calculator', 'arguments': '{"expression": "3 * 24.5 + 2 * 41.0 + 129.0"}'}]
# tool {"result": 284.5}
# assistant Three lamps, two chairs and a desk cost 284.5 euros.
# Three lamps, two chairs and a desk cost 284.5 euros. 1.7 2.5 4
# 530

# a flaky tool: the runtime retries with backoff; the model never sees the transient failures
answer, _, clock, _, attempts = agent('How much do three lamps, two chairs and a desk cost?', np.random.default_rng(4), flaky=0.3)
print(answer, round(clock, 2), attempts)
print(round(1 - 0.3 ** 3, 3))                                      # success within 3 independent attempts
# Three lamps, two chairs and a desk cost 284.5 euros. 2.3 5
# 0.973

# 5. constrained decoding: a toy vocabulary, and a checker that the output can still become valid
SEGS = [('lit', ['{"item": "']), ('lit', list(PRICES)), ('lit', ['", "qty": ']), ('int', None), ('lit', ['}'])]
def match(s, i=0, k=0, partial=True):
    """Can s[i:] be read as segments k, k+1, ... (only a prefix of them if partial)?"""
    if i == len(s): return partial or k == len(SEGS)
    if k == len(SEGS): return False
    kind, alts = SEGS[k]
    if kind == 'lit':
        for v in alts:
            n = min(len(v), len(s) - i)
            if s[i:i + n] == v[:n] and (match(s, i + n, k + 1, partial) if n == len(v) else partial): return True
        return False
    j = i                                                           # an integer: one or more digits
    while j < len(s) and s[j].isdigit():
        j += 1
        if match(s, j, k + 1, partial): return True
    return False

@lru_cache(maxsize=None)
def live(s, depth=0):
    """Can some sequence of vocabulary tokens complete s? (a legal character prefix is not enough)"""
    if match(s, partial=False): return True
    nxt = [w for w in VOCAB if w != '<eos>' and match(s + w)]
    return depth < 8 and any(live(s + w, depth + 1) for w in nxt)

VOCAB = ['{"', 'item', '": "', '{"item": "', 'lamp', 'lamps', 'chair', 'desk', 'de', 'sk', '", "qty": ',
         '"', '3', '12', '}', 'Sure', '<eos>']                                      # '"' alone leads to a dead end
def toy_logits(prefix):
    z = dict.fromkeys(VOCAB, -4.0)
    if prefix == '':                          z.update({'Sure': 1.0, '{"': 1.0, '{"item": "': 1.2})
    elif prefix.endswith('{"'):               z.update({'item': 3.0})
    elif prefix.endswith('item'):             z.update({'": "': 3.0})
    elif prefix.endswith('"item": "'):        z.update({'lamps': 2.0, 'lamp': 1.5, 'Sure': 0.5, 'chair': 0.2, 'de': 0.0, 'desk': -0.5, '}': -1.0})
    elif prefix.endswith('de'):               z.update({'sk': 3.0})
    elif re.search(r'"(lamps?|chair|desk)$', prefix): z.update({'", "qty": ': 2.0, '"': 1.0, '}': 0.5})
    elif prefix.endswith('": '):              z.update({'3': 2.0, '12': 1.0})
    elif re.search(r'\d$', prefix):           z.update({'}': 2.0, '12': 0.0})
    else:                                     z.update({'<eos>': 3.0})
    return np.array([z[w] for w in VOCAB])

def decode(rng, constrained, tau=1.0, max_tokens=12):
    out = ''
    for _ in range(max_tokens):
        z = toy_logits(out) / tau
        if constrained:                                             # mask every token that makes completion impossible
            ok = [match(out, partial=False) if w == '<eos>' else match(out + w) and live(out + w) for w in VOCAB]
            z = np.where(ok, z, -np.inf)
        p = np.exp(z - z.max()); p /= p.sum()
        w = VOCAB[rng.choice(len(VOCAB), p=p)]
        if w == '<eos>': break
        out += w
    return out

def is_valid(s):
    try: x = json.loads(s)
    except json.JSONDecodeError: return False
    schema = {'type': 'object', 'properties': {'item': {'type': 'string', 'enum': list(PRICES)}, 'qty': {'type': 'integer'}},
              'required': ['item', 'qty'], 'additionalProperties': False}
    return not validate(x, schema)

rng = np.random.default_rng(0)
for constrained in (False, True):
    outs = [decode(rng, constrained) for _ in range(2000)]
    print(constrained, np.mean([is_valid(s) for s in outs]), outs[:2])
# False 0.1575 ['{"item": "lamps", "qty": sk', 'Sure']
# True 1.0 ['{"item": "chair", "qty": 3}', '{"item": "lamp", "qty": 3}']

# the masking step after '{"item": "': logits, allowed tokens, probabilities before and after
z = toy_logits('{"item": "')
ok = np.array([w != '<eos>' and match('{"item": "' + w) and live('{"item": "' + w) for w in VOCAB])
p = np.exp(z) / np.exp(z).sum()
q = np.where(ok, p, 0) / p[ok].sum()
for w in ('lamps', 'lamp', 'Sure', 'chair', 'de', 'desk', '}'):
    i = VOCAB.index(w); print(f'{w:6s} {z[i]:5.1f} {int(ok[i])} {p[i]:.3f} {q[i]:.3f}')
print('other', round(float(p[~np.isin(VOCAB, ['lamps', 'lamp', 'Sure', 'chair', 'de', 'desk', '}'])].sum()), 3))
# lamps    2.0 0 0.437 0.000
# lamp     1.5 1 0.265 0.613
# Sure     0.5 0 0.098 0.000
# chair    0.2 1 0.072 0.167
# de       0.0 1 0.059 0.137
# desk    -0.5 1 0.036 0.083
# }       -1.0 0 0.022 0.000
# other 0.011

# 6. masking token by token is not conditioning on validity: valid strings 'ab' and 'cd' only
p1 = {'a': 0.5, 'c': 0.5}; p2 = {'a': {'b': 0.1}, 'c': {'d': 0.9}}     # P(second token | first); the rest is invalid
joint = {f + s: p1[f] * p2[f][s] for f in p1 for s in p2[f]}
print({k: round(v / sum(joint.values()), 3) for k, v in joint.items()}, {f + s: p1[f] for f in p1 for s in p2[f]})
# {'ab': 0.1, 'cd': 0.9} {'ab': 0.5, 'cd': 0.5}

Next#

The model can now reason, and it can act through tools. What remains is the loop that combines the two over many steps: when to think and when to act, how to read what comes back, when to stop, and what the loop costs as its context grows. The next note builds that loop from the ReAct paper, compares it with acting without reasoning and reasoning without acting, and works through a complete trace.

References#