liveBook · Manning

CodeAgent

As explained in Domain-Specific Small Language Models

A smolagents agent pattern that generates Python code to invoke tools and solve a task step by step.

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

How Domain-Specific Small Language Models covers it

How deep it goes
Explains the execution pattern, tool interfaces, configuration, retrieval integration, and sandbox boundary without detailing interpreter internals.
Examples
Demonstrates flight booking, local-document search, LanceDB semantic retrieval, hybrid retrieval, and web-search tool selection.
What it assumes
Assumes intermediate Python and LLM application knowledge while teaching agent components, tool exposure, planning, and execution from their operational roles.
What you can do after
Build it from scratch with smolagents by configuring a model, exposing typed tools, setting execution limits, and running multistep tasks.
How it connects
Connects ReAct, tool calling, Python, retrieval-augmented generation, BM25, semantic search, hybrid search, LangChain, and LanceDB.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.