← All tools

Developer & Compute

Code Execution

Run code in a sandboxed runtime.

code_execution

Overview

Runs a small snippet of JavaScript or shell code inside a safe sandbox. Handy when the agent needs to do a quick calculation or transform some data that's too complex for the calculator but too small for a full script.

How it works

Executes the supplied snippet in either GraalVM JavaScript or /bin/sh, with a configurable timeout (default 30s, hard cap 60s). Dangerous commands and writes to system directories are blocked. Returns stdout combined with any return value from JavaScript execution.

Example

When a user asks:

Compute the compound return from these quarterly returns.

the agent calls the tool:

code_execution(language="javascript", code="returns.reduce((a,b)=>a*(1+b),1) - 1")

and gets back: the computed number, returned as text.

Use it in a workflow

Wire this tool into a SwarmAI crew. Use the YAML DSL for declarative workflows, or the Java builder API when you want full programmatic control.

YAML DSL

# compute-helper.yaml
name: compute-helper-crew
process: SEQUENTIAL

agents:
  - id: runner
    role: Compute Helper
    goal: Run small scripted transforms inline
    tools:
      - code_execution

tasks:
  - id: compute-helper-task
    agent: runner
    description: Compute the compound return for the returns array [0.03, 0.04, -0.01, 0.06].

Java

import ai.intelliswarm.swarmai.agent.Agent;
import ai.intelliswarm.swarmai.task.Task;
import ai.intelliswarm.swarmai.swarm.Swarm;
import ai.intelliswarm.swarmai.swarm.SwarmOutput;
import ai.intelliswarm.swarmai.process.ProcessType;
import ai.intelliswarm.swarmai.tool.common.CodeExecutionTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;

@Autowired ChatClient chatClient;
@Autowired CodeExecutionTool codeExecutionTool;

Agent runner = Agent.builder()
    .role("Compute Helper")
    .goal("Run small scripted transforms inline")
    .chatClient(chatClient)
    .tool(codeExecutionTool)
    .build();

Task runnerTask = Task.builder()
    .description("Compute the compound return for the returns array [0.03, 0.04, -0.01, 0.06].")
    .agent(runner)
    .build();

SwarmOutput result = Swarm.builder()
    .agent(runner)
    .task(runnerTask)
    .process(ProcessType.SEQUENTIAL)
    .build()
    .kickoff();

What it's good for

Real scenarios where agents put this tool to work.

Data-transform snippets inside analyst agents
Quick scripted validations during reasoning
Sandboxed JS evaluation for formulas
Glue-code steps between other tools

Source

Implementation lives at swarmai-tools/src/main/java/ai/intelliswarm/swarmai/tool/common/CodeExecutionTool.java in the swarm-ai repository.

Open code_execution on GitHub →