Key Concept: Code Agents
Instead of outputting JSON tool calls, smolagents agents write Python code:
from smolagents import CodeAgent, tool, HfApiModel
@tool
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"Sunny, 22C in {city}"
agent = CodeAgent(tools=[get_weather], model=HfApiModel())
agent.run("What's the weather in Tokyo?")The agent writes: result = get_weather("Tokyo") and executes it.
Why Code Agents?
- More expressive — loops, conditionals, variables
- Composable — chain tool calls naturally
- Debuggable — read the generated code
- Efficient — fewer LLM calls than ReAct