skills/dspy-codeact/SKILL.md
Use when the agent task is best solved by writing and executing Python code — data manipulation, computation, file processing, or tasks where code is more reliable than natural language reasoning. Common scenarios - data analysis tasks where the agent writes pandas code, computation-heavy tasks where natural language reasoning fails, file processing automation, tasks requiring precise calculations, or building agents that manipulate data programmatically. Related - ai-taking-actions, dspy-react, dspy-program-of-thought. Also used for dspy.CodeAct, agent writes and runs Python code, code execution agent, data analysis agent, AI agent that writes code, computation with LLM agent, pandas automation with AI, agent that processes files, code-generating agent, execute Python in sandbox, when ReAct is not precise enough use code, programmatic problem solving agent.
npx skillsauth add lebsral/dspy-programming-not-prompting-lms-skills dspy-codeactInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Guide the user through building DSPy agents that solve problems by generating and running Python code, rather than calling tools through a fixed interface.
dspy.CodeAct is a DSPy module that creates agents which write Python code to accomplish tasks. Instead of selecting from a predefined set of tool calls (like ReAct), CodeAct generates executable code snippets that use provided tools as Python functions.
The agent works in a loop:
CodeAct inherits from both ReAct and ProgramOfThought, combining reasoning-and-acting with code generation.
| | ReAct | CodeAct | |---|---|---| | How it acts | Calls tools by name with arguments | Writes Python code that calls tools | | Composition | One tool call per step | Can chain multiple tool calls, use loops, variables, conditionals in a single step | | Data manipulation | Limited to what tools return | Can transform, filter, aggregate data in code | | Best for | Simple tool orchestration | Complex computation, data processing, multi-step logic | | Overhead | Lower -- just picks a tool | Higher -- generates and executes code |
Rule of thumb: If your agent needs to do math, transform data, or chain several operations together, CodeAct is a better fit. If it just needs to look things up and combine results, ReAct is simpler.
Use CodeAct when the agent needs to:
Avoid CodeAct when:
import dspy
lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
# Define tools as pure functions with type hints and docstrings
def factorial(n: int) -> int:
"""Calculate the factorial of n."""
if n <= 1:
return 1
return n * factorial(n - 1)
def fibonacci(n: int) -> int:
"""Return the nth Fibonacci number."""
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
# Create the CodeAct agent
agent = dspy.CodeAct(
"question -> answer",
tools=[factorial, fibonacci],
max_iters=5,
)
result = agent(question="What is the factorial of 10 plus the 15th Fibonacci number?")
print(result.answer)
dspy.CodeAct(
signature, # str or dspy.Signature -- defines input/output fields
tools, # list[callable] -- pure functions the agent can call in code
max_iters=5, # int -- max generate-execute cycles before stopping
interpreter=None, # PythonInterpreter or None -- custom interpreter (creates one if None)
)
signature -- same as any DSPy module. Defines what the agent receives and what it should produce.tools -- a list of Python functions. Must be pure functions (not callable objects or class instances). All dependencies must be self-contained within each function -- tools cannot import external libraries or reference outside state.max_iters -- safety limit on how many code-generation-and-execution cycles the agent runs. Default is 5. Increase for complex multi-step tasks, decrease for simple ones.interpreter -- optionally pass a pre-configured dspy.PythonInterpreter. If None, CodeAct creates a fresh one. The interpreter runs code in a sandboxed Deno-based environment.CodeAct is strict about what tools it accepts:
# OK -- pure function
def search(query: str) -> str:
"""Search for information."""
return "results..."
# OK -- function with multiple parameters
def lookup(table: str, key: str) -> str:
"""Look up a value in a table."""
return "value..."
# NOT OK -- callable object (will be rejected)
class MyTool:
def __call__(self, query: str) -> str:
return "results..."
# NOT OK -- tool that imports external libraries
def analyze(data: str) -> str:
"""Analyze data."""
import pandas as pd # This will fail in the sandbox
return str(pd.read_csv(data))
Rules for tools:
CodeAct runs generated code in a sandboxed Deno-based Python interpreter, not your system's Python. This means:
The sandbox provides security boundaries, but your tool functions themselves run in your normal Python process. If a tool accesses a database or API, that access is real.
Tool functions are the trust boundary. The sandbox constrains the generated glue code, but tool functions execute with full privileges. Keep tool functions minimal and validate their inputs.
Set max_iters appropriately. A runaway agent burns tokens. Start with max_iters=5 and increase only if you see the agent running out of steps on legitimate tasks.
Validate outputs. Use dspy.Refine as a wrapper to check that the agent's answer meets your requirements via a reward function.
Don't expose dangerous operations as tools. If you pass a tool that deletes files or sends emails, the agent can and will call it. Only expose tools you're comfortable with the agent using autonomously.
class SafeCodeAgent(dspy.Module):
def __init__(self, tools):
self.agent = dspy.CodeAct(
"task -> result",
tools=tools,
max_iters=5,
)
def forward(self, task):
return self.agent(task=task)
def non_empty_reward(args, pred):
if len(pred.result.strip()) > 0:
return 1.0
return 0.0 # Agent must produce a non-empty result
validated_agent = dspy.Refine(
module=SafeCodeAgent(tools=[]),
N=3,
reward_fn=non_empty_reward,
threshold=1.0,
)
Wrap CodeAct in a dspy.Module to add pre-processing, post-processing, or combine it with other DSPy modules:
class AnalysisAgent(dspy.Module):
def __init__(self):
self.planner = dspy.ChainOfThought("task -> plan")
self.executor = dspy.CodeAct(
"task, plan -> result",
tools=[compute_stats, format_table],
max_iters=8,
)
self.summarize = dspy.ChainOfThought("task, result -> summary")
def forward(self, task):
plan = self.planner(task=task)
execution = self.executor(task=task, plan=plan.plan)
return self.summarize(task=task, result=execution.result)
CodeAct agents are optimizable like any DSPy module:
def task_metric(example, prediction, trace=None):
return prediction.answer.strip() == example.answer.strip()
# BootstrapFewShot works well -- the agent learns from successful code traces
optimizer = dspy.BootstrapFewShot(metric=task_metric, max_bootstrapped_demos=4)
optimized = optimizer.compile(agent, trainset=trainset)
# MIPROv2 can also tune the instructions for code generation
optimizer = dspy.MIPROv2(metric=task_metric, auto="medium")
optimized = optimizer.compile(agent, trainset=trainset)
# Save and load
optimized.save("optimized_codeact.json")
| Scenario | Use | |---|---| | Look up facts and combine them | ReAct | | Calculate, aggregate, or transform data | CodeAct | | Simple API calls (weather, stock price) | ReAct | | Multi-step data processing pipeline | CodeAct | | Tasks where approach varies per input | CodeAct | | Quick prototype with many tools | ReAct | | Math-heavy or logic-heavy problems | CodeAct | | Tasks needing external libraries in tools | ReAct (more flexible tool format) |
__call__), not bound methods, not lambdas. If you need to wrap state, define a closure that captures the state and pass the inner function.import pandas between tool calls, it fails. Move all library usage inside the tool function itself, not in the generated code.tools list. Claude often defines helper functions but forgets to register them.max_iters too low for complex tasks. The default max_iters=5 is fine for simple computation, but data analysis tasks that fetch multiple sources and process results often need 8-10 iterations. Watch for "max iterations reached" and increase accordingly.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/ai-taking-actions/ai-taking-actions/ai-coordinating-agents/dspy-modules/dspy-signatures/ai-do if you do not have it — it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-dotools
See what is happening during optimizer.compile() instead of waiting blind. Use when you want to watch optimization progress, see scores as they come in, know if your optimizer is working, check if optimization is stuck, understand why optimization is taking too long, get live progress during compile, monitor convergence, detect overfitting during optimization, interpret optimization results, or pick the right tool for watching optimization. Also used for optimizer progress bar, is my optimizer doing anything, optimization seems stuck, how long will optimization take, watch GEPA run, watch MIPROv2 run, live optimization dashboard, optimizer not improving, scores not going up, optimization taking forever, see what optimizer is doing, debug slow optimization, optimization visibility, optimizer metrics, track compile progress, optimization observability.
testing
Use when you want the highest-quality prompt optimization DSPy offers — jointly optimizes instructions and few-shot demos, with auto=light/medium/heavy presets. Common scenarios - you want the best possible accuracy from prompt optimization, jointly tuning instructions and few-shot demonstrations, using auto presets for different compute budgets, or when COPRO or BootstrapFewShot alone are not reaching your accuracy target. Related - ai-improving-accuracy, dspy-copro, dspy-bootstrap-few-shot. Also used for dspy.MIPROv2, best DSPy optimizer, highest quality optimization, auto=light medium heavy, joint instruction and demo optimization, most powerful prompt optimizer, MIPROv2 vs COPRO vs BootstrapFewShot, which optimizer should I use, state of the art prompt optimization, when to use MIPROv2, optimize both instructions and examples, heavy optimization for production, best optimizer for accuracy.
testing
Use LangWatch for DSPy auto-tracing and real-time optimizer progress. Use when you want to set up LangWatch, langwatch.dspy.init, auto-tracing DSPy, real-time optimization dashboard, optimizer progress tracking, app.langwatch.ai, or DSPy optimizer dashboard. Also used for langwatch setup, pip install langwatch, langwatch trace, optimizer progress, real-time optimization, watch optimizer run, LangWatch self-hosted, langwatch docker, langwatch vs langtrace, langwatch autotrack_dspy.
data-ai
Use when you want to optimize instructions without few-shot examples — a lightweight alternative to COPRO when you do not have or do not want to use demonstrations. Common scenarios - optimizing instructions when you do not have or do not want to use few-shot demonstrations, lightweight instruction search as a first step, tasks where examples in the prompt confuse the model, or when you want fast instruction optimization without the cost of COPRO. Related - ai-improving-accuracy, dspy-copro, dspy-miprov2. Also used for dspy.GEPA, instruction optimization without demos, lightweight prompt optimization, optimize instructions only, no few-shot examples needed, GEPA vs COPRO, quick instruction search, when demonstrations hurt performance, zero-shot optimization, instruction-only optimizer, simplest instruction tuner, fast prompt optimization, skip few-shot and just tune instructions, optimize Pydantic field descriptions, GEPA structured output, GEPA does not optimize field desc.