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:
- 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). - 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. - The
callConnectorBridge: When the sandboxed code needs external data, it callscallConnector("/path").kenpachihandles 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 tonew 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(...):
- LLM Decision: The model analyzes the user query, recognizes
calculate_discounted_checkout, and extracts{ productId: "prod_99182", eurExchangeRate: 0.92 }. - Sandbox Execution:
kenpachispins up an isolated V8 container (node:vm) and executes thejsBodycode. - Connector Bridge:
callConnector("/products/prod_99182")is intercepted bykenpachi. The server fetches the data fromhttps://api.yourstore.com/v1/products/prod_99182with theSTORE_API_SECRET_KEYattached. - 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 ofhttps://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.