Skip to content

Vol. III · Single-Agent Patterns · III.6

Code-Act

The model writes a program to use tools, instead of emitting one JSON call at a time. Hugging Face smolagents made this the default for a reason: code composes.

6 min read

Doctrine

  • Code-act needs a sandbox. Always. The action space is a language runtime.
  • It wins when the task is compositional (loops, filters, pandas) rather than a single API poke.
  • Keep the library surface small. An agent that can import anything will.

Why code beats JSON for some tasks

JSON tool calling is a shallow DSL. Python is a deep one. Filtering a table, retrying with backoff, plotting — these are clumsy as fifteen tool calls and natural as fifteen lines. smolagents' bet is that models are better at Python than at proprietary JSON dialects.

OpenHands and Aider edit code as the product; smolagents uses code as the tool glue. E2B is how you survive either choice.

Anti-patterns

  • exec() on the host.
  • Exposing the full standard library 'for convenience'.

Related