Skip to main content
The most powerful BoxLite pattern: let an LLM generate code and execute it in a sandbox. The LLM reasons about the problem, writes Python, and BoxLite runs it safely. This tutorial shows how to wire up tool calling with three popular providers.

How it works

Every LLM provider supports tool calling (also called function calling). You define a tool called execute_python that accepts a code parameter. When the LLM decides it needs to compute something, it returns a tool call instead of a text response. Your code executes that tool call inside a BoxLite CodeBox and feeds the output back to the LLM, which then formulates the final answer. The pattern is the same regardless of provider:
  1. Define the tool schema (what “execute code” means)
  2. Send messages to the LLM with the tool available
  3. When the LLM returns a tool call, run the code in BoxLite
  4. Send the result back to the LLM
  5. Repeat until the LLM responds with text

Prerequisites

OpenAI

openai_sandbox.py
Expected output:

Anthropic

Anthropic uses a different tool schema format (input_schema instead of parameters) and a different response structure (stop_reason instead of finish_reason, content blocks instead of tool_calls).
anthropic_sandbox.py

Vercel AI SDK

The Vercel AI SDK provides a unified tool() helper that handles schema validation and execution in one place. The maxSteps parameter replaces the manual while loop — the SDK automatically re-calls the model when a tool result is returned.
vercel-ai-sandbox.ts
The Vercel AI SDK supports many model providers — swap openai('gpt-4o') for anthropic('claude-sonnet-4-5-20250929'), google('gemini-2.0-flash'), or any other supported model. The tool definitions stay the same.

Multiple questions

The CodeBox persists across calls, so installed packages and files on disk carry over between tool invocations within the same conversation. Note that in-memory state (variables, functions) does not persist — each run() is a separate Python process.
multi_question.py
For production deployments — concurrency models, timeout handling, security presets, and defensive execution patterns — see the AI agent integration guide.

What’s next?

Automate a browser

Use BrowserBox to give your LLM agent web browsing capabilities.

AI agent integration

Production patterns: timeouts, security presets, and concurrency.

Upload & download files

Pass data files to your sandbox and retrieve results.

CodeBox API reference

Full API docs for CodeBox.