Agent Systems: Tool Use and Code Execution
Pattern reference. This is a short design note, not a standalone lesson. For the worked argument, step-by-step practice, and assessment, read the full course chapter.
In one sentence. Act through typed interfaces that return explicit observations, so side effects are authorized and checkable rather than asserted.
Problem
Language generation is not the right mechanism for deterministic operations, external state, or exact computation. The agent needs a controlled way to act and to receive an observation.
Intent and structure
agent → tool schema → capability → observation → agent
Function calling, MCP-style tool protocols, HTTP services, databases, code runners, and proof assistants are implementations of this boundary. The stable part is the tool name, input schema, permissions, return schema, and error behavior.
Code execution
Use code for exact arithmetic, symbolic experiments, finite searches, plots, or formal proof checking. Keep the program, environment, and output as inspectable artifacts. A successful execution is evidence about the computation, not automatically a proof of the surrounding theorem.
Safety and reliability
Use least privilege, validate arguments, control side effects, set timeouts, make writes idempotent where possible, and distinguish a tool error from a negative mathematical result. An observation must be checked before it changes the next state.
Example
A theorem-search action should return inspected source passages and locators. A Lean-checking action should return the exact declaration, environment, and actual checker output. For a named system, inspect its identified version before claiming that it exposes or restricts either capability.
Mathematical system design: bind observations to the question actually computed
Record exact inputs, arithmetic mode, software version, and the translation from mathematical objects to code.
Hands-on practice
Compare floating-point samples of f(x)=x with the exact witness argument. State precisely what each output establishes.
Deliverable. Save the input, intermediate artifacts, expected outcome, and actual outcome. Include one negative case that the system must reject. Use the runnable ledger lab as a starting point; do not treat its bookkeeping checks as a proof checker.
Reference basis
The tool-use pattern is Gulli, Agentic Design Patterns, Chapter 5, and Lakshmanan and Hapke, Generative AI Design Patterns, where tool calling and code execution are Patterns 21 and 22. The reasoning/action interface is exemplified by the ReAct paper; standardized access to external data and tools is specified by the MCP specification. Function calling as an implementation, and its parallel form, are treated practically in James Phoenix and Mike Taylor, Prompt Engineering for Generative AI, O’Reilly, 2024, Chapter 4. For the packaging of tools and instructions as named, loadable capabilities — so that a capability’s detail enters context only when it is needed — see Stephen Clear, Claude AI Bible, Chapters 14 and 16.
Worked design decision: interpret the tool result before reusing it
A symbolic calculation may return zero because of simplification rules, hidden assumptions, or a restricted domain. Preserve the exact expression, assumptions, command, and output. Then explain why that computation supports the mathematical inference in this problem.
Acceptance exercise. Test a nearby input outside the declared assumptions. If the tool still produces output, the interpretation layer must prevent an unsupported conclusion. Numerical agreement is evidence for investigation, not a general proof.