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
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
Switching to the Code Agent
To use the Code Agent instead of the default Function Calling Agent, simply setagent_type to "code-agent" in your task configuration. This is the only change required to switch between agent types.
Example
How it works
The Code Agent generates Python code that:- Calls tools programmatically: Instead of making individual tool calls, the agent can write code that calls multiple tools in loops or conditionals
- Stores intermediate results: Uses variables to store tool outputs and reuse them in subsequent operations
- Handles errors gracefully: Can catch exceptions and retry operations with different approaches
- Processes data efficiently: Uses Python’s data structures and libraries to transform and analyze data from tool outputs

