Overview

8 Empowering agents with code execution

The chapter introduces a shift from predefined tool calling to giving agents a true execution environment. Instead of forcing an LLM to choose from a fixed list of narrow functions, the agent can write and run code, which greatly expands what it can do. This matters because predefined tools limit the action space, create inefficiency through repeated LLM-tool-observation cycles, and bloat the prompt as more tools are added. Code execution solves many of these problems by letting the model express loops, conditionals, data reuse, and multi-step workflows in a single program.

A major focus is making code execution safe and practical by connecting agents to sandboxed environments such as E2B. Sandboxes isolate code from the host system, reducing security risk, limiting resource consumption, and making recovery easy if something goes wrong. The chapter explains how to integrate this into the agent framework by storing the sandbox in execution context, auto-registering an execution tool, managing sandbox lifecycle cleanly, and returning structured results and errors so the model can debug and retry. It also extends the idea beyond raw code execution by allowing existing pure function tools to be ported into the sandbox, so code written by the LLM can call them directly.

The chapter then broadens the environment into a full workspace with file system access and CLI support, which better matches how human developers work. Tools like bash execution and file upload let the agent analyze local files, install packages, run shell commands, and process data in an isolated Linux environment without needing specialized custom tools for every task. Finally, it introduces Agent Skills as a hierarchical way to reduce prompt size and tool confusion: only brief skill summaries are included up front, while detailed instructions are loaded on demand from files in the workspace. Together, these ideas form a layered system where core tools enable code execution, skills provide reusable capabilities, and generated code handles complex task-specific behavior efficiently.

Chapter 8 position in the book: providing agents with complete workspaces.
Comparison of different tool execution approaches: from simple tool calling to tool creation through code.
CodeAct achieves higher success rates with fewer interactions compared to JSON and text formats.
Tool calling vs. code execution: repeated tool calls require 31 LLM iterations, while code execution handles the same task in a single LLM call.
Workspace tools: the agent calls bash and file tools to interact with the CLI and file system inside the E2B Sandbox.
Agent Skills folder structure showing SKILL.md and associated scripts(source: https://www.anthropic.com/engineering/equipping-agents-for-the-real-world-with-agent-skills).
YAML Frontmatter in SKILL.md providing name and description for prompt inclusion
Level 1: Skill summary list in system prompt showing minimal token usage(source: https://www.anthropic.com/engineering/equipping-agents-for-the-real-world-with-agent-skills).
Agent Skills implementation architecture showing SkillInfo, loader, and Agent integration.

Summary

  • Predefined tool approaches have three limitations: the agent's action space is restricted to the developer-defined tool list, executing one tool per LLM call is inefficient, and as tools proliferate, prompt token costs increase while the LLM's tool selection accuracy decreases.
  • Code execution provides three core capabilities: control flow through conditionals and loops, data flow by storing intermediate results in variables, and tool composition by combining small functions to create new functionality.
  • Sandboxes are isolated environments for safely executing LLM-generated code, providing host system security protection, resource management for CPU and memory, and recoverability when problems occur.
  • Tool composition can be implemented by extracting the source code of user-defined tools and registering them in the sandbox. This approach maximizes token efficiency when processing large amounts of data while only needing aggregation or summaries.
  • Workspace is a complete working environment combining code execution, file system, and CLI. Just as human developers write code, save files, and execute commands in the terminal, agents can work in the same environment.
  • Agent Skills is an approach that progressively discloses tool information. Only skill names and one-line descriptions are included in the prompt, while actual usage is learned when the LLM determines a skill is needed and reads the SKILL.md file.
  • Agent tools can be divided into three layers: core tools always included in the prompt (execute_python, bash_tool, upload_file), installable tools with only summaries in the prompt and detailed definitions in the file system (Agent Skills), and generated tools where the LLM writes code on the spot as needed.
  • The essence of the hierarchical tool structure is offloading tool definitions from the prompt to Workspace. From a context engineering perspective, Workspace is a powerful method for removing tool information and tool execution results that don't require LLM judgment from the context.

FAQ

What is the main limitation of the predefined tool approach?The predefined tool approach limits the agent to a fixed set of developer-defined tools, so it cannot handle unexpected or complex tasks unless the needed capability was already implemented in advance.
Why is code execution more powerful than calling tools one by one?Code execution lets the LLM write a program that can use control flow, variables, and tool composition to complete a task in one execution, instead of requiring many separate tool calls and LLM turns.
How does code execution improve efficiency for multi-step tasks?It reduces the number of LLM interactions by allowing repeated actions, branching, and data processing to happen inside the sandbox, which lowers latency and can reduce token usage.
What are the three core capabilities that code brings to agents?The chapter highlights three key capabilities: control flow for branching and loops, data flow for storing intermediate results in variables, and tool composition for building more complex functionality from smaller tools.
Why are sandboxes necessary for LLM code execution?Sandboxes isolate execution from the host system, helping prevent damage from dangerous or buggy code, limiting resource usage, and making it easier to recover from failures.
What does E2B provide for agent code execution?E2B provides an isolated cloud sandbox where agents can create an environment, run Python code, keep state across executions, pass environment variables, and safely terminate the session when done.
How is code execution integrated into the Agent class?The Agent is extended with a code_execution option, an ExecutionContext field for storing the sandbox, automatic registration of the execute_python tool, and sandbox lifecycle management during run().
What is the purpose of porting tools to the sandbox?Porting tools to the sandbox allows LLM-generated code to call existing functions directly inside the execution environment, enabling efficient batch processing and reducing repeated LLM calls.
What kinds of tools can be marked as sandbox_executable?Only pure FunctionTool tools that do not require ExecutionContext can be marked sandbox_executable, because they must be definable and callable inside the sandbox independently.
How do Agent Skills reduce token usage compared to listing all tools in the prompt?Agent Skills show only short names and descriptions in the prompt at first, and detailed instructions are loaded only when needed, which greatly reduces prompt size and avoids exposing unnecessary tool definitions.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build an AI Agent (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build an AI Agent (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build an AI Agent (From Scratch) ebook for free