interactive · 12 min

A Tool-Using Loop

Objective: wire a tool registry into the loop — the policy chooses a tool name and the runtime executes the registered function from a registry you control.

Model

Mock is the default and free. Switch the Model toggle above to Real (your key) to run this same code on your own OpenAI-compatible model.

This MockLLM is the same seam from Foundations — the loop structure never trusts the model to run anything; it maps a chosen name to a vetted function in TOOLS. Unknown names fall back to a plain answer. Note that each tool unpacks its own arguments (add splits the comma-packed string) — argument parsing is the tool’s job, not the dispatcher’s. Whether mock or real, the dispatch logic — parsing name: arg, looking up TOOLS, and acting — stays the same; only the line producing decision changes.

Best practice: the model selects a tool by name from a registry you control; argument parsing and execution are your code, not the model’s.

Next: Tool Errors & Retries