Today we're sharing research that bridges two powerful paradigms in AI agent design: the expressiveness of code-based actions and the reliability of structured generation. Our findings show that forcing CodeAgents to generate both thoughts and code in a structured JSON format can significantly outperform traditional approaches across multiple benchmarks.

Figure 1: Accuracy comparison of three approaches: Structured CodeAgent (blue), CodeAgent (orange), and ToolCallingAgent (gray) on SmolBench (GAIA, MATH, SimpleQA, and Frames). Error bars represent 95% Confidence Intervals.

In a recent development, AI agents need to take actions in the world - whether that's calling APIs, processing data, or reasoning through complex problems. How agents express these actions has evolved through several paradigms:.

Traditional JSON Agent: Agents generate structured JSON to call tools. (Related: How to generate text: using different decoding methods ...)

Sources indicate that these agents operate by selecting from a list of predefined tools and generating JSON-formatted calls. This method for calling tools has been popularized by OpenAI's function calling API, and has since then been the most widely used method to call tools.

It is reliable, but limited by:.

Code Agents: Agents make use of their innate coding ability and write executable Python code directly.

This shift, first presented as CodeAct in the paper “Executable Code Actions Elicit Better LLM Agents” gave AI agents the flexibility to write arbitrary executable Python code in addition to tool-calling.

Sources indicate that the key insight here is that tools are called directly from within the code, making variables and state management much more reliable. Agents can call tools within loops, functions, and conditional statements - essentially generating a dynamic graph of tool execution in each action!

However, parsing code from markdown can be error-prone which leads us to a proposition: why not use structured generation to generate code actions? (Related: Latest Update: Retrieval Augmented Generation with Hugg...)

With Structured outputs, you can force the LLM to generate explicit thoughts and code as a JSON blob:.

The key difference is that the generation is enforced: basically, now instead of just being prompted to output thoughts, then code, the usage of structured outputs forces it to respect the structure.

This approach adds the reliability of structured generation to the flexibility of code execution, thus getting the best of both worlds.

Recent findings suggest that we compared these three paradigms across multiple benchmarks including GAIA, MATH, SimpleQA, and Frames. The results show a clear pattern: Code actions + structured generation consistently improves performance for capable models.

Across most capable models, the structured approach consistently outperformed the regular CodeAgent approach by 2-7 percentage points on average.

Our implementation of CodeAgent in smolagents extracts Python code from the LLM output, which can fail when:.

Structured generation eliminates these complications with reliable JSON parsing.

Recent findings suggest that to understand why structured generation matters, we analyzed 15,724 agent traces across our benchmarks. The results are striking:.

Agent traces without parsing errors succeed 21.3% more often than those with parsing errors.

This isn't just about convenience - parsing errors create a cascade of failures that significantly impact overall agent performance. When an agent can't execute its first action due to malformed code, it often struggles to recover, leading to suboptimal problem-solving paths.

Figure 2: Parsing errors in the first step reduce success rates of the agent by 21.3% and increase average steps taken from 3.18 to 4.63.

Additionally: Enforced Reasoning Process.

According to recent reports, the use of structured generation and explicit thoughts not just prompts, but forces agents to articulate their reasoning before acting. This leads to:.

It has been revealed that our results also reveal a clear capability threshold: models need sufficient instruction-following ability and JSON coverage in their pre-training data to benefit from structured generation. This suggests that structured approaches work best with:.

Here's what happens when a smaller model (e.g mistralai/Mistral-7B-Instruct-v0.3) tries to generate structured code - the cognitive load becomes too much:.

The model generates syntactically broken Python code: web_search(query="Eiffel Tower height")", - notice the malformed string with an extra quote and comma. This leads to an immediate SyntaxError and execution failure.

Sources indicate that this illustrates the "structure tax": smaller models struggle to simultaneously handle JSON formatting, Python syntax, and the actual problem-solving logic. The cognitive overhead of structured generation can overwhelm models that would otherwise perform reasonably well with simpler markdown-based code generation.

✅ Use Structured CodeAgents when:.

In a recent development, it’s super simple! Just enable it with use_structured_outputs_internally:.

The LLM will generate something like this:.

Then the "code" part gets executed by the agent as usual : this is the standard CodeAgent, but now it has 100% parsing reliability!

Recent findings suggest that this research suggests we're moving toward a more nuanced understanding of agent architectures. It's not just about "what can the agent do?" but "how should the agent think about what it's doing?".

Maybe making the reasoning process more explicit helps the model stay on track. Or maybe it's just easier to parse. Either way, it’s a win.

Sources indicate that but this is just the beginning. There are so many questions left to explore:.

It has been revealed that for now, if you're using smolagents (or building your own CodeAgent system), consider giving structured output a try. Your parsing errors will thank you, and you might just see a nice increase in performance!

Did you try YAML block scalars or Python triple-quoted strings? These require less escaping, so they might mitigate the "structure tax".

AI agents the flexibility to write arbitrary executable Python code in addition to tool-calling. The key insight here is that tools are called directly from within the code, making variables and state management much more reliable. Agents can call tools within loops, functions, and conditional statements - essentially generating a dynamic graph of tool execution in each actionhttps://spacebar-clicker.org/.

I found the "structure tax" concept particularly interesting - it makes total sense that smaller models would struggle with the cognitive overhead of maintaining both JSON structure and Python syntax simultaneously. That Mistral-7B example with the malformed string really drives the point home. https://cpsgames.org/.

→ Read original article