Code Agents
Agents that use code to perform tool calls
Code Agents are a new kind of agent that writes code to perform different actions. They’re changing how we think about AI tool usage.
Traditional agents make individual function calls—like asking “What’s 2+2?” then “What’s 4+4?” then “What’s 8+8?” Each question requires a round trip to the model.
Code agents think differently. They write a script: “Let me calculate powers of 2 up to 8.” One model call, multiple operations.
Why Code Agents Matter
There are several key benefits to code agents:
- Token Efficiency: Saving on precious internet tokens (you don’t need as many tool calls to your agent)
- Better Accuracy: More reliable for complicated tasks
- Speed: Batch operations in single calls
Here’s a graphic from the Executable Code Actions Elicit Better LLM Agents paper which demonstrates why code agents are so valuable:
Building an Election Data Agent
Let’s see code agents in action by building an agent that can analyze election data. We’ll use the Hugging Face smolagents library, which has built-in support for code agents.
Our agent will query a DuckDB database containing Florida Election data to answer complex demographic questions.
Setting Up the Tools
In smolagents
, defining tools is straightforward:
This tool gives our agent direct access to a DuckDB in-memory database where it can create tables, insert data, and run complex queries—all through code.
Creating the Agent
Unlike traditional agents that make individual tool calls, the CodeAgent
writes and executes code blocks that can use multiple tools in sequence.
Simple Example: Hello World
Let’s start with a simple test:
The agent thinks through the problem, then writes code to solve it:
Thought: To echo “hello world” from the database, I need to create a table, insert the string “hello world” into it, and then retrieve it using a SELECT query.
Code:
Three database operations in one LLM call. Traditional agents would need three separate calls to achieve the same result—code agents batch the work efficiently.
Complex Analysis: Voter Demographics
Now let’s tackle something more challenging. We’ll ask our agent to analyze voter demographics:
How the Agent Approaches the Problem
Watch how the code agent breaks down this complex question into manageable steps. Unlike traditional agents that would need multiple back-and-forth calls, our code agent plans the entire analysis upfront and executes it systematically:
Step 1: Data Discovery
The agent starts by exploring the database schema to understand what’s available. It runs PRAGMA table_info('voters')
to discover the table has 40 columns including birth_date
and party_affiliation
—exactly what it needs.
Step 2: Age Group Analysis
Next, it calculates voter counts by age group using SQL’s DATE_DIFF
function to determine ages from birth dates:
View Age Group Analysis SQL
Step 3: Complex Statistical Analysis Finally, it builds a sophisticated query using window functions to calculate party registration percentages within each age group—this is where the real power of SQL shines:
View Party Breakdown SQL
The Results
The 65+ age group has the most voters (27,965), followed by 55-64 (15,310) and 35-44 (12,750).
Key findings from the analysis:
- Republican (REP) registration is highest across all age groups
- Republican percentage increases with age: 54.7% (18-24) to 63.4% (55-64)
- No Party Affiliation (NPA) is second highest in younger groups but decreases with age
- Democratic (DEM) registration is consistently around 14-16% except for 65+ where it jumps to 22.1%
This shows a clear pattern where older voters tend to be more Republican-leaning, while younger voters have higher rates of unaffiliated registration.
The Power of Code Agents
This example demonstrates several key advantages of code agents:
Complex Analysis: The agent performed multi-step data analysis that would require several back-and-forth exchanges with traditional function-calling agents.
Efficiency: Instead of multiple API calls, the agent planned and executed the entire analysis in just a few steps.
Flexibility: Code agents can adapt their approach based on the data structure they discover, writing custom SQL queries as needed.
Rich Insights: The agent not only answered the direct question but provided deeper insights about voting patterns across demographics.
Code agents represent a significant advancement in AI tooling, enabling more sophisticated and efficient problem-solving capabilities that bridge the gap between simple function calls and complex analytical workflows.