CodeAgent is a smolagents pattern in which an LLM writes and executes Python snippets at each step to analyze a task, call configured tools, and iteratively produce an answer. It follows the ReAct pattern while expressing actions as executable code rather than JSON instructions. section 13.4 section 13.5
A working CodeAgent combines:
- An LLM, a memory manager, and one or more tools; smolagents can connect to Hugging Face, OpenAI-compatible, LiteLLM, Ollama, and other endpoints. section 13.4
- Python tools exposed through type hints and docstrings, or custom Tool subclasses defining names, descriptions, inputs, outputs, and execution logic. section 13.5 section 14.2
- A bounded execution loop controlled with options such as max_steps and verbosity_level, allowing multistep tool chaining. section 13.4
- An execution boundary: local execution restricts tools, safe functions, and imports by default, while Docker or E2B provides safer production isolation. section 13.4
Flight search, seating, and booking
A CodeAgent can receive “Find a flight from Dublin to New York and then select a seat,” generate Python calling search_for_flights, pass the selected flight to find_seat, and optionally call book_flight_ticket. With three tools and three steps, the workflow returns a mocked ticket such as flight DUB-JFK789, seat B26, and a ticket number. section 13.4
Agentic retrieval
For local documents, the agent can choose BM25 keyword search, semantic search over LanceDB embeddings, or a weighted hybrid retriever. It can also select web search when the query requires it, generating Python for the chosen tool. section 14.2 section 14.4
Figure 13.5 The logical workflow of CodeAgent
Use the workflow diagram to trace how the model, memory, tools, generated Python, and execution results form an iterative agent loop. section 13.4
