# CodeMode lets an agent write Python that calls your tools directly

> CodeMode gives an agent a persistent Python kernel in place of a long list of individual tool calls. The model gets execute and restart, writes real Python, and calls your tools as awaitable handles.

- Published: 2026-08-24
- Author: Ashpreet Bedi
- Categories: Changelog
- Canonical: https://www.agno.com/articles/codemode-lets-an-agent-write-python-that-calls-your-tools-directly
- Markdown: https://www.agno.com/articles/codemode-lets-an-agent-write-python-that-calls-your-tools-directly.md

`CodeMode` is a new toolkit that gives an agent a persistent Python kernel in place of a long list of individual tool calls. The model gets two tools, `execute` and `restart`. It writes Python in `execute` and calls your tools as awaitable handles, composing them with variables, loops, and helper functions in a single cell.

Previously, an agent with many tools worked one call at a time, round-tripping each result through the transcript and filling the context window with intermediate output. Now that composition happens in the kernel, and anything the model set up, from a variable to a parsed tool result, stays available across turns. Writing real code also lets the model use actual control flow instead of approximating it call by call.

`CodeMode` needs extra dependencies that aren't in base Agno. Install them with `pip install 'agno[code]'`, which pulls in `ipykernel`, `jupyter_client`, and `dill`. Without them, importing the toolkit raises an `ImportError`.

It is also not a sandbox and does not pretend to be one. It runs Python and shell with the same permissions as the process running your agent, and it restores session snapshots by unpickling them, so resuming a session also executes code. Run it with a trusted operator or inside an isolated container, and keep untrusted input away from it.

```python
# pip install 'agno[code]'
from agno.agent import Agent
from agno.tools.code.code_mode import CodeMode

agent = Agent(tools=[CodeMode(tools=[...])])
```

See the [cookbook](https://github.com/agno-agi/agno/tree/main/cookbook/code/01_basics) for reference.
