← Inference From First Principles

Tool Calling and Agentic Inference

Generation, interruption, tool handoff, re-entry.

Chapter 33 examined reasoning budgets. This chapter examines tool use.

Tool calling

Tool calling lets a model request external computation. The runtime must parse the request, execute safely, and return results. The model proposes calls; the application is responsible for authorisation, execution, validation, and retries.

Interruption and re-entry

Generation pauses for tool execution, then resumes with the tool result. This requires state management and trace IDs.

Sandboxing

Tools must be sandboxed. Untrusted model output must not execute arbitrary code. Tool arguments and results are untrusted data requiring schemas and policy checks.

Research context

OpenAI and Anthropic documentation frame tool use as a proposal/execution loop: the model emits a structured call, the application validates and executes it, then returns a tool result. Harmony adds a protocol-level representation for compatible models. The application remains responsible for safety; the protocol does not make tool output trusted.

Experiment

The repository example implements a toy calculator tool with trace IDs and retries. Results were 4, 42, and error. Tests verify calculation, trace IDs, unknown tools, and serialisation.

Limitations

  • Toy calculator.
  • No real tool execution.
  • No end-to-end task measurement.
  • No security audit.

Key terms

  • Tool calling: requesting external computation.
  • Trace ID: unique request identifier.
  • Sandbox: restricted execution environment.
  • Re-entry: resuming generation.
  • Retry: repeated attempt.

Reader exercises

  1. Add a file-read tool with allowlist.
  2. Implement retry backoff.
  3. Measure end-to-end task completion.
  4. Explain risks of unsandboxed tools.

References

Implementation status

  • Manuscript: content/books/inference/34-chapter.md
  • Runnable example: examples/ch34/
  • Tests: tests/test_ch34_tool.py
  • Evidence: research/ch34/sources.md and research/ch34/results.json
  • Verification command: python -m unittest discover -s tests -v

The chapter is a draft. It does not claim production tool safety. The next chapter will examine structured outputs.