Skip to main content
When building autonomous agents, pre-defining every tool ahead of time is impossible. Users will inevitably ask your agent to perform specialized calculations, data formatting, or custom logic that you didn’t code in advance. Sandboxed Tool Runtime enables kenpachi agents to author small, custom JavaScript functions on the fly while running. To protect your system, generated code executes in an isolated V8 sandbox (node:vm) with zero direct network access and no exposure to secret API keys.
When should you use this? Use sandboxed tool execution when your agent needs to perform custom math, parse strings, or run logic tailored to a user request without cluttering your codebase with static tools.

How It Works (The Safe vs. The Sandbox)

To keep your backend secure, kenpachi strictly separates API Credentials from Model Logic:
  1. The Connector (Your Safe): Lives on your backend server. It securely maps an endpoint alias (like "store_api") to host environment variables (process.env.STORE_API_KEY).
  2. The V8 Sandbox (The Isolated Room): The AI model writes pure algorithmic JavaScript. The sandbox prevents the code from accessing your host filesystem, running require(), or making unauthorized internet requests.
  3. The callConnector Bridge: When the sandboxed code needs external data, it calls callConnector("/path"). kenpachi handles the request on the host server, injects the secret key, and returns the result—the LLM never sees or touches your private API key.

Real-World Example: End-to-End Shopping Assistant

Imagine a shopping assistant where a user asks for a price calculation in Euros. The agent attaches the sandboxed tool and evaluates the response using an LLM.

Step 1: Register the API Endpoint (Server-Side)

Register your store API endpoint in your server initialization code:
server.ts

Step 2: Pass the Tool to an Agent and Run

Instead of manually executing tools, pass the synthesized tool to new Agent() and let agent.run() trigger the LLM call and sandboxed execution automatically:
app.ts

What Happens During agent.run()?

When you execute agent.run(...):
  1. LLM Decision: The model analyzes the user query, recognizes calculate_discounted_checkout, and extracts { productId: "prod_99182", eurExchangeRate: 0.92 }.
  2. Sandbox Execution: kenpachi spins up an isolated V8 container (node:vm) and executes the jsBody code.
  3. Connector Bridge: callConnector("/products/prod_99182") is intercepted by kenpachi. The server fetches the data from https://api.yourstore.com/v1/products/prod_99182 with the STORE_API_SECRET_KEY attached.
  4. Final Response: The computed result (78.20 EUR) is passed back to the LLM to format the final user-facing response.

What callConnector Handles Automatically

Inside the jsBody code block:
  • No full URLs: Write clean relative paths like /products/... instead of https://api.yourstore.com/v1/products/....
  • No header management: Authorization tokens and secret keys are injected on the server side automatically.
  • No network leaks: The sandbox cannot make unapproved fetch() requests to outside domains.

Comparison