Skip to main content

What You’ll Learn

  • Defining a @tool with crewai.tools that executes Python in a Declaw sandbox
  • Creating a CrewAI Agent, Task, and Crew
  • Demo mode that exercises the sandbox tool directly without needing an OpenAI key

Prerequisites

  • Declaw instance running and DECLAW_API_KEY / DECLAW_DOMAIN set
  • OPENAI_API_KEY (optional — the example runs in demo mode without it)
pip install declaw python-dotenv crewai
This example is available in Python. TypeScript support coming soon.

Code Walkthrough

1. Define the Declaw sandbox tool

Use the crewai.tools @tool decorator with a display name as the first argument:
from crewai.tools import tool
from declaw import Sandbox

@tool("Execute Python Code")
def execute_python(code: str) -> str:
    """Execute Python code securely in a Declaw sandbox and return the output."""
    sbx = Sandbox.create(template="python", timeout=300)
    try:
        sbx.files.write("/tmp/code.py", code)
        result = sbx.commands.run("python3 /tmp/code.py", timeout=30)
        return f"{result.stdout}\n{result.stderr}".strip()
    finally:
        sbx.kill()
CrewAI uses the docstring as the tool’s description when presenting it to the LLM. Keep it concise and accurate.

2. Create an Agent with the tool

from crewai import Agent

coder = Agent(
    role="Python Developer",
    goal="Write and execute Python code to solve tasks",
    backstory="You are an expert Python developer with access to a secure sandbox.",
    tools=[execute_python],
    verbose=True,
)

3. Define a Task and run the Crew

from crewai import Task, Crew

task = Task(
    description="Compute the sum of all prime numbers below 50 and print the result.",
    expected_output="The sum of all prime numbers below 50.",
    agent=coder,
)

crew = Crew(agents=[coder], tasks=[task], verbose=True)
result = crew.kickoff()
print(f"Final result: {result}")

4. Demo mode (no API key needed)

Run the sandbox tool directly to verify it works before hooking it up to CrewAI:
from declaw import Sandbox

code = """\
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

primes = [x for x in range(50) if is_prime(x)]
print(f"Primes below 50: {primes}")
print(f"Sum: {sum(primes)}")
"""

sbx = Sandbox.create(template="python", timeout=300)
try:
    sbx.files.write("/tmp/code.py", code)
    result = sbx.commands.run("python3 /tmp/code.py", timeout=30)
    print(result.stdout)
finally:
    sbx.kill()

Expected Output

In demo mode:
=======================================================
CrewAI + Declaw Sandbox Example
=======================================================
No OPENAI_API_KEY found -- running demo mode.

--- Tool Definition ---
from crewai.tools import tool
from declaw import Sandbox

@tool("Execute Python Code")
def execute_python(code: str) -> str:
    ...

--- Running Code Directly in Declaw Sandbox ---
Code:
def is_prime(n): ...

stdout: Primes below 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Sum: 328
stderr:
exit_code: 0

Sandbox cleaned up.

=======================================================
Done!
=======================================================