What Code Execution Enables
Code Execution is the right tool when determinism, precision, or structured API handling matters:- Calling third-party APIs with structured request and response handling — authentication headers, error checking, retry logic
- Transforming and validating JSON payloads — reshaping, filtering, and normalizing complex data structures
- Running deterministic calculations — scoring models, weighted rankings, currency conversions, statistical computations
- Building lightweight data pipelines — fetch, transform, filter, and return in a single execution
How It Works
When Code Execution is enabled on an agent, the agent decides on its own whether writing code is the right approach for a given task. Here’s the full flow:- The agent receives a task and determines that code is the most reliable path
- The agent writes JavaScript or TypeScript to accomplish it
- The sandboxed runtime executes the code under strict resource limits
- The sandbox returns stdout, the return value, and any errors — the same feedback a developer would get in a terminal
- The agent uses that output to respond to the user, pass results to another tool, or take the next step
Enable Code Execution on an Agent
1
Open your agent
Navigate to your agent in Scout Studio.
2
Go to the Tools tab
Click the Tools tab in the agent editor.
3
Enable Code Execution
Toggle on Code Execution from the native tools list.
4
Save and test
Save your agent and send a prompt that requires a calculation, data transform, or API call. Review the Logs tab to see the code the agent generated and the output it received.
A Real Example
Say your agent receives this prompt: “Fetch the top five accounts from our CRM API and score them by revenue.” The agent generates and executes something like this:JavaScript
Prompt Examples
These prompts naturally lead an agent to use Code Execution:Instruction Snippet
Add this block to your agent’s instructions to guide when and how it uses Code Execution:Consistent Output Shapes
Give your agent a standard structure for both success and failure responses. Consistent shapes make downstream tool chaining predictable and make it easier for both the agent and humans to understand what happened. Success:ok: true/false as a top-level field lets the agent branch on results without parsing error strings — and makes log review much faster.
Security Model
Code Execution runs in a sandboxed runtime with strict resource limits. Your agent’s code cannot:- Access the filesystem outside the sandbox
- Make outbound network calls to arbitrary destinations (unless explicitly allowed by your configuration)
- Spawn persistent processes or background workers
- Access other agents’ data or Scout’s internal systems
Code Execution vs. Workflow JavaScript Block
Scout offers two places to run JavaScript. Here’s when to use each:
Use Agent Code Execution when the agent needs to adapt its approach based on what it discovers. Use the Workflow JavaScript Block when the same logic runs identically every time and you want it reviewed, versioned, and locked.
Best Practices
Validate inputs before running. Instruct the agent to check that required fields exist and have expected types before executing code that depends on them. Keep outputs small and structured. Return only what’s needed for the next step — not the entire raw API response. This keeps context clean and downstream processing fast. Handle errors explicitly. Check HTTP status codes, catch exceptions, and return structured error objects rather than letting the code throw unhandled errors. Avoid side effects unless requested. Don’t write code that modifies external systems (CRM records, databases, emails) unless the user explicitly asked for that action. Log key decisions. Include asummary field in your return value explaining what the code did. This makes log review significantly faster when debugging.