Skip to main content
The Code Agent uses executable Python code as actions, enabling AI models to interact with tools and environments through a unified code-based action space. Unlike traditional approaches that use JSON or text-based actions, the Code Agent generates Python code that can be executed directly, providing greater flexibility and efficiency for complex tasks.

Key advantages

The Code Agent offers several advantages over traditional function-calling approaches:
  • Control and data flow: Code inherently supports programming constructs like if-statements, for-loops, and variables, allowing the agent to compose multiple tools in a single action and store intermediate results for reuse
  • Dynamic execution: Integrated with a Python interpreter, the agent can execute code actions and dynamically revise prior actions or emit new actions based on observations and feedback
  • Self-debugging: The agent can leverage automated feedback (e.g., error messages) from code execution to improve task-solving by debugging its generated code
  • Leverage existing libraries: Code actions allow the agent to use readily available Python packages for an expanded action space instead of relying solely on pre-defined tools
  • Efficiency: For complex tasks requiring multiple tool calls, the Code Agent can achieve the same results with up to 30% fewer actions compared to traditional approaches
Research shows that code-based agents can achieve up to 20% higher success rates on complex multi-step tasks that require intricate coordination and composition of multiple tools (CodeAct paper).

When to use the Code Agent

Use the Code Agent when you need to:
  • Handle complex multi-step workflows that require coordinating multiple tools or APIs
  • Process data programmatically using loops, conditionals, and data transformations
  • Compose multiple operations in a single action (e.g., processing a list of items with the same sequence of tools)
  • Leverage Python libraries for data processing, calculations, or other operations
  • Perform tasks requiring self-correction where the agent needs to debug and fix errors based on execution feedback
  • Optimize action efficiency for tasks that would otherwise require many sequential tool calls
The Code Agent is particularly effective for tasks that benefit from programming constructs like loops, conditionals, and variable management, making it ideal for batch processing, data transformation, and complex logical operations.

Switching to the Code Agent

To use the Code Agent instead of the default Function Calling Agent, simply set agent_type to "code-agent" in your task configuration. This is the only change required to switch between agent types.

Example

from lumo_sdk import LumoClient, Message, MessageRole

# Initialize the client
client = LumoClient(api_key="your-api-key-here")

# Use Code Agent for complex multi-step task
response = client.run_task(
    task="Search for the top 5 AI companies and get their current stock prices",
    model="gpt-4.1-nano",
    base_url="https://api.openai.com/v1/chat/completions",
    tools=["ExaSearchTool", "VisitWebsite"],
    agent_type="code-agent",
)

How it works

The Code Agent generates Python code that:
  1. Calls tools programmatically: Instead of making individual tool calls, the agent can write code that calls multiple tools in loops or conditionals
  2. Stores intermediate results: Uses variables to store tool outputs and reuse them in subsequent operations
  3. Handles errors gracefully: Can catch exceptions and retry operations with different approaches
  4. Processes data efficiently: Uses Python’s data structures and libraries to transform and analyze data from tool outputs
For example, instead of making 10 separate tool calls to process 10 items, the Code Agent can write a single for-loop that processes all items efficiently in one action.