smolagents
smolagents is a minimal library that lets you build powerful agents in a few lines of code. Its design goals:
- Simplicity. The core agent logic is ~1,000 lines — you can read all of it.
- Code agents first.
CodeAgentwrites its actions as Python code instead of JSON tool calls. Research shows this is more reliable and more token-efficient than JSON blobs. - Model-agnostic. Use any LLM — an Inferix endpoint, a local
transformersmodel, an OpenAI-compatible API, or 100+ providers via LiteLLM. - Tool-agnostic. Wrap any Python function as a tool, or import tools from the Hub, LangChain, or an MCP server.
- Secure. Run agent-generated code in a sandbox (E2B, Docker, or WebAssembly).
How a code agent works
A CodeAgent runs a ReAct loop. At each step it writes a snippet of Python, that code is executed, and the observations feed the next step — until it calls final_answer(...).
Thought: I should search for the current population, then compute the ratio.
Code: pop = web_search("Guangzhou population 2024")
print(pop)
Observation: 18.7 million ...
Continue to Installation, then the Quickstart.