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 theMOONSHOT_API_KEY environment variable before running the code:
Run generated code in your tool-calling loop
PassRUN_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
In a real application, how does generated code access my own data or functions?
In a real application, how does generated code access my own data or functions?
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:- 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.
- 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.
Do I have to use Docker as the execution environment?
Do I have to use Docker as the execution environment?
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.