Applied AI·topic 5 of 12
Structured outputs and tool calling
The bridge between free text and code you can actually run. This is the most-used capability in production LLM features and the subject of scripts 4 and 5.
Read in order · tick what you already know
- 01
you need JSON your code can parse rather than prose, so you constrain the shape of the response instead of writing a regex over the answer.
Structured output
- 02
the response parsed cleanly and still had the wrong keys in it, because the flag only ever promised valid JSON.
JSON mode
- 03
you write out the types, required fields and enums once, and use the same thing for tool parameters and for the response shape.
JSON schema
- 04
you measure what fraction of a thousand runs actually validated against the schema, and that rate is the reliability number you report.
Schema conformance
- 05
at each step, delete every next-token option that would break the format, then sample from what remains.
Constrained decoding
- 06
you hand the local model a grammar and it cannot emit anything outside your format, not even a helpful preamble.
Grammar-based decoding
- 07
log every retry; a rising retry rate is an early warning that a prompt or model change degraded reliability.
Validation and retry loop
- 08
the model replies with the name of one of your functions and its arguments instead of an answer, and your code runs it and hands the result back.
Function calling / tool calling
- 09
the model kept picking the wrong tool until you rewrote the description like documentation for a junior engineer.
Tool definition / schema
- 10
you force one specific tool so the model has to answer in that schema instead of deciding to chat about it.
Tool choice
- 11
you send the output back referencing the call it answers, and when it failed you send the error as structured content rather than nothing.
Tool result
- 12
a while loop around an HTTP call, with a switch statement over tool names in the middle.
The tool loop
- 13
you stop capping it at one tool call and let the model keep calling tools until it decides it is finished.
Agentic loop
- 14
the model asked for three lookups in one turn and you ran them concurrently instead of one after another.
Parallel tool calls
- 15
the model called the same write tool twice, and it only mattered because you had not made the write idempotent.
Side effects and idempotency in tools
- 16
a USB standard for model tools; implement the server once, every compatible client can plug in.
Model Context Protocol (MCP)