DS DevShelfHub Projects · AI tools
Tutorials / CrewAI / Reference / Classes / CodeInterpreterTool
Class tools

CodeInterpreterTool: Reference Guide

By DevShelfHub

Executes Python code in a sandbox — analyses data, performs calculations, generates plots.

See the CrewAI API reference index, CrewAI introduction, and core concepts for surrounding context.

What is CodeInterpreterTool?

CodeInterpreterTool exposes a Python execution environment to the LLM so numeric work, quick data transforms, and matplotlib-style plots can happen outside the model weights. CrewAI wraps a sandboxed interpreter process with guardrails compared to raw exec, but it is still remote code execution driven by an imperfect planner — treat it as privileged capability reserved for trusted inputs and tightly scoped tasks.

Operationally, pair it with read-only data ingress (FileReadTool, constrained SQL tools) and strict output channels so agents cannot exfiltrate environment variables. For multi-tenant SaaS or internet-facing bots, prefer dedicated isolation products such as E2B-backed tools rather than the stock interpreter. Monitor disk and CPU: plotting large frames or downloading data inside the sandbox can blow memory limits on small containers.

When upgrading crewai_tools, revalidate your prompts: minor changes in sandbox paths or allowed stdlib modules can break previously working notebooks. Snapshot dependency versions alongside crew definitions for reproducible research crews.

When to Use

Trusted internal analytics crews, spreadsheet-style crunching, or prototyping where operators control all inputs.

Use Cases

  • Data analysis
  • Quick calculations
  • Plotting for internal reports

Key Features

  • Sandboxed interpreter
  • Pairs with file tools
  • Great for deterministic math

When NOT to Use

Untrusted multi-tenant user code or internet-exposed agents — use stronger isolation offerings.

Notes

Prompt injection is code injection

Any user-controlled string that reaches the agent can steer it toward malicious Python. Combine tool gating, input redaction, and human review for workflows that accept external documents.

Resource limits

Plots and large pandas operations can exhaust RAM in small pods. Set Kubernetes memory limits, chunk data in prior tasks, or move heavy compute to a dedicated worker service.

Determinism vs LLM creativity

Ask the model to show code and results, but validate critical numbers with fixed offline tests. The interpreter faithfully runs what the model wrote — garbage in, garbage out.

Import

python
from crewai_tools import CodeInterpreterTool

Code Examples

Minimal wiring

python
from crewai import Agent
from crewai_tools import CodeInterpreterTool

analyst = Agent(
    role='Analyst',
    goal='Answer with computed numbers',
    backstory='You show Python you used.',
    tools=[CodeInterpreterTool()],
    allow_code_execution=True,
)

Combine with FileReadTool for CSV summaries

python
from crewai import Agent, Task, Crew, Process
from crewai_tools import CodeInterpreterTool, FileReadTool

analyst = Agent(
    role='Analyst',
    goal='Summarize sales.csv numerically',
    backstory='You load data via tools, never hallucinate rows.',
    tools=[FileReadTool(), CodeInterpreterTool()],
    allow_code_execution=True,
)

task = Task(
    description='Read sales.csv and report mean/median revenue by region.',
    expected_output='Markdown table',
    agent=analyst,
)

Crew(agents=[analyst], tasks=[task], process=Process.sequential).kickoff()

Cap risk with allow_code_execution flags

python
# Prefer code_execution_mode='safe' (default on Agent) and keep CodeInterpreterTool off public bots.
from crewai import Agent
from crewai_tools import CodeInterpreterTool

internal_only = Agent(
    role='Quant helper',
    goal='Run trusted scripts',
    backstory='You never access network APIs from code.',
    tools=[CodeInterpreterTool()],
    allow_code_execution=True,
    code_execution_mode='safe',
)

Common Mistakes

❌ Running untrusted code without isolation

✅ Use E2B-backed execution tools or refuse CodeInterpreterTool for public-facing agents.

❌ Omitting allow_code_execution on the Agent while expecting tool output

✅ Enable allow_code_execution (and safe mode) so the agent can actually run interpreter-backed steps.

CodeInterpreterTool FAQ

What is CodeInterpreterTool in CrewAI?

Executes Python code in a sandbox — analyses data, performs calculations, generates plots. CodeInterpreterTool exposes a Python execution environment to the LLM so numeric work, quick data transforms, and matplotlib-style plots can happen outside the model weights. CrewAI wraps a sandboxed interpreter process with guardrails compared to raw exec, but it is still remote code execution driven by an imperfect planner — treat it as privileged capability reserved for trusted inputs and tightly scoped tasks. Operationally, pair it with read-only data ingress (FileReadTo…

Which package defines the CrewAI class CodeInterpreterTool?

DevShelfHub maps CodeInterpreterTool to Python module crewai_tools (package path crewai_tools in this reference). Pin your installed crewai version and match imports to the snippet on this page.

When should I use CodeInterpreterTool?

Trusted internal analytics crews, spreadsheet-style crunching, or prototyping where operators control all inputs.

When should I avoid using CodeInterpreterTool?

Untrusted multi-tenant user code or internet-exposed agents — use stronger isolation offerings.

How do I import CodeInterpreterTool in Python?

from crewai_tools import CodeInterpreterTool

Where can I explore more CrewAI API reference pages?

Open the CrewAI API reference index on DevShelfHub to search 58 classes, 30 methods, and 16 decorators, each with runnable examples, parameters, common mistakes, and cross-links.