Skip to main content
When a task requires querying records in batches, passing data across tools, or filtering and aggregating multiple items, having the model issue standard tool calls one by one adds extra round trips and fills the messages history with raw records. Instead, you can define a client-side run_code tool so the model can write a short Python script that loops over tool functions and computes the result inside an isolated container, returning only the final aggregated output to the model.

Implement the client-side code execution tool

This example builds on the tool-calling workflow. Make sure Docker is installed locally and set your API key in the MOONSHOT_API_KEY environment variable before running the code:
The table below summarizes each part of the code:

Run generated code in your tool-calling loop

Pass RUN_CODE_TOOL in tools. When the model returns a run_code call, execute the generated Python code in the container and append the output back to messages:

Frequently asked questions

The example embeds ORDERS and helper functions in SANDBOX_TOOLS for demonstration. In production, generated code can access your business data in two common ways:
  1. Pass a data snapshot: Before starting the sandbox, serialize the records needed for the current step as JSON and pass them in through standard input or a read-only file. The generated script operates only on the snapshot and never accesses external data. This approach works best when the required dataset is small and known in advance.
  2. Forward queries through a controlled bridge: Place only lightweight stub functions inside the sandbox. When generated code calls a stub, the stub forwards the request to your host service over a local socket or internal read-only endpoint, and the host runs the real query and returns the result. Database passwords and API keys stay on the host and never enter the sandbox. This approach is better suited to large datasets or workflows that need to retrieve data during computation.
Model-generated code must run in an isolated environment. Calling exec() in the host process runs that code with host privileges: it can read and write host files and read credentials from environment variables such as MOONSHOT_API_KEY, and an infinite loop or memory spike could stall the host process.The example uses a network-disabled disposable Docker container (--rm --network none) because Docker is the easiest way to run an isolated environment locally. In production, you can swap in a faster isolated runtime such as a V8 isolate, WebAssembly sandbox, Firecracker microVM, or managed code sandbox service. Whichever runtime you choose, it must meet three requirements: generated code cannot access host credentials, each run has a timeout, and execution errors are returned to the model as regular tool results.