Give Claude a sandbox with the Anthropic SDK
anthropicTools(box) turns a sandbox into tools for the Anthropic Messages
API and runs each tool_use block Claude sends in the sandbox. You keep your
own loop and your own Anthropic key. To run Claude Code itself in the sandbox
instead, see Run a coding agent.
Install
npm install @anthropic-ai/sdk @tangle-network/sandboxSet ANTHROPIC_API_KEY for the Anthropic SDK and TANGLE_API_KEY for the
sandbox.
Run a tool loop
import Anthropic from "@anthropic-ai/sdk";
import { Sandbox } from "@tangle-network/sandbox";
import { anthropicTools } from "@tangle-network/sandbox/agent";
const claude = new Anthropic();
const client = new Sandbox({
apiKey: process.env.TANGLE_API_KEY!,
baseUrl: "https://sandbox.tangle.tools",
});
const box = await client.create();
try {
const { tools, handleAssistantMessage } = anthropicTools(box, { sessionId: "chat-1" });
const messages: Anthropic.MessageParam[] = [
{ role: "user", content: "Plot sin(x) for x in [0, 2π] and describe the plot." },
];
for (let step = 0; step < 6; step++) {
const reply = await claude.messages.create({
model: "claude-opus-5-5",
max_tokens: 4096,
tools,
messages,
});
messages.push({ role: "assistant", content: reply.content });
if (reply.stop_reason !== "tool_use") break;
messages.push({ role: "user", content: await handleAssistantMessage(reply.content) });
}
} finally {
await box.delete();
}Use any Claude model id from Anthropic’s model list.
What happens on each step:
- Claude calls
sandbox_run_codewithlanguage: "python"and the source. - The sandbox runs it in a kernel for session
chat-1. Variables stay alive between calls. - The result carries typed
results. A matplotlib figure arrives as a base64 PNG, which the pack sends to Claude as a native image block.
Derive sessionId from your conversation id, so one conversation keeps one
kernel.
Choose the tools
const { tools, handleAssistantMessage } = anthropicTools(box, {
sessionId: "chat-1",
allow: ["sandbox_run_code", "sandbox_read", "sandbox_write"],
});The default set is sandbox_run_code, sandbox_exec, sandbox_read,
sandbox_write, sandbox_list and sandbox_search. Preview tools are off
unless you name them in allow.
handleToolUse(block) runs one tool_use block, and handleAssistantMessage
runs every block in a message in parallel.
Streaming
With claude.messages.stream(...), collect the content blocks and call
handleAssistantMessage(content) once the message is complete. Claude
finishes each tool_use block before the message ends.
Errors
An unknown tool name, or an error thrown while running a tool, comes back as a
tool_result block with is_error: true and the message as its content, so
Claude can react.
Types
import type {
AnthropicTool,
AnthropicToolPack,
AnthropicToolPackOptions,
AnthropicToolUseBlock,
AnthropicToolResultBlock,
CodeExecutionResult,
CodeResultPart,
} from "@tangle-network/sandbox/agent";AnthropicToolUseBlock is what Claude emits:
{ type: "tool_use", id, name, input }. AnthropicToolResultBlock is what
you send back: { type: "tool_result", tool_use_id, content, is_error? }.
The same entry exports anthropicLifecycleTools and anthropicFleetTools,
which let Claude create sandboxes and fleets itself. See
Integrations.
For retries, kernel state across restarts and duplicate tool calls, see Retries and duplicate work.