liveBook · Manning

Chapter 8: Empowering agents with code execution

What you will learn in chapter 8 of Build an AI Agent (From Scratch)

The introduction contrasts predefined tools with giving an agent an environment where its LLM can write and run code.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction contrasts predefined tools with giving an agent an environment where its LLM can write and run code.

Key terms: predefined tool approaches, execution environment, code execution, give an agent a computer

8.1 Giving agents a computer

This section explains why predefined tool calls constrain agents and introduces direct code execution as an alternative.

Key terms: tool-calling approach, predefined functions, action space, generate and execute code

8.1.1 Limitations of the predefined-tool approach

This section explains why an agent limited to predefined tools can get stuck, incur repeated LLM calls and context costs, and struggle with a growing tool list.

Key terms: predefined-tool approach, action space, execution environment, LLM call, tool execution, observation, context, latency, tool proliferation, tool definitions

8.1.2 What code execution brings

This section explains how code execution lets an agent complete tasks more efficiently and handle more complex work through control flow, intermediate data, and tool composition.

Key terms: code-execution capability, LLM call, code execution, predefined-tool approach, control flow, conditionals, loops, business logic, data flow, intermediate results

8.1.3 The effectiveness of code-based actions

This section uses CodeAct research to show that expressing agent actions as code can improve task success and reduce LLM interactions compared with JSON or text actions.

Key terms: CodeAct, JSON format, text format, code format, interactions, intermediate results, variables, conditionals, loops, plan and procedure

8.2 Connecting code environments

This section introduces the upcoming work of adding code execution to the agent using a sandbox and testing the integration.

8.2.1 Why sandboxes are necessary

This section explains why LLM-generated code should run in a sandbox, what protections isolation provides, and why the book uses E2B.

Key terms: LLM-generated code, prompt injection, sandbox, isolation, resource management, recoverability, Docker containers, cloud sandbox services, E2B, state persistence

8.2.2 Introduction to E2B

This section introduces the E2B sandbox workflow and explains how to inspect results, configure execution, reuse state, and recover from code errors.

Key terms: E2B, API key, .env file, E2B package, Sandbox.create(), run_code(), execution object, kill(), execution.logs.stdout, execution.results

8.2.3 Connecting the sandbox to the agent

This section connects an E2B sandbox to the Agent so code execution is available through a tool, shares execution state, and cleans up reliably.

Key terms: code_execution, e2b, _setup_tools(), execute_python, ExecutionContext, code_env, sandbox, Optional, run(), sandbox life cycle

8.2.4 Testing the code-execution agent

A Fibonacci test shows how a code-execution agent combines the LLM’s choice of code with Python’s precise calculation.

Key terms: code-execution agent, LLM’s reasoning ability, precise calculation capability

8.3 Porting tools to sandboxes

This introduction identifies a limitation of sandboxed code execution: code in the sandbox cannot directly call tools built earlier in the book.

Key terms: sandbox, tools, code execution

8.3.1 The need for tool portability

This section uses a historical Wikipedia query to show why sandbox-executed code can be more efficient than repeated agent tool calls and why local tools need a way to run in an isolated sandbox.

Key terms: last version from June 2023, Wikipedia’s revision API, regular agent tool, sandbox-executed code, LLM call, agent’s context, small extracted outputs, token use, E2B sandbox, isolated environment

8.3.2 Extending FunctionTool

This section narrows sandbox tool support to suitable FunctionTools and explains the flag, validation, and source-code extraction needed to make them available there.

Key terms: FunctionTool, ExecutionContext, pure functions, sandbox, sandbox_executable, context parameter, get_source_code(), inspect.getsource(), @tool decorator, sandbox registration

8.3.3 Implementing the Wikipedia revision tool

This section shows how a Wikipedia revision tool retrieves historical page versions and why its function must be self-contained to run in a sandbox.

Key terms: action=query, prop=revisions, rvstart, sandbox_executable=True, pure function, context parameter, structured data

8.3.4 Handling sandbox tools in the Agent

This section shows how the Agent separates sandbox-executable tools, makes their functions available in the sandbox, and tells the LLM how to call them.

Key terms: Agent, _setup_tools(), sandbox_executable, code execution, sandbox, source code, LLM-generated code, system prompt, tool signatures, descriptions

8.3.5 Practical testing

Shows how a sandbox-executable tool processes a Wikipedia example and explains when code-based tool use is worth its overhead.

Key terms: sandbox_executable, get_wikipedia_revisions, sandbox, context, aggregations or summaries, dependent tool calls, intermediate data, code execution overhead, regular tool calling

8.4 Workspace: Using filesystems and the CLI

This introduction explains why agents need a workspace in addition to code execution.

Key terms: code execution, workspace, terminal, intermediate results

8.4.1 The workspace concept

This section defines an agent workspace, explains how agents can create file-handling actions on demand, and shows how E2B exposes CLI and filesystem access.

Key terms: workspace, code execution, filesystem, CLI, list_files, os.listdir(), grep, E2B sandbox, commands.run(), files.write()

8.4.2 Implementing workspace tools

This section explains how to give an agent CLI and file-transfer access to an E2B sandbox and how those workspace tools are registered.

Key terms: bash_tool, commands.run(), _format_command_result, stdout, stderr, exit codes, upload_file, local file path, destination path, /home/user/

8.4.3 Practical example: Analyzing an Excel file

This section shows how an agent uses sandboxed workspace tools to analyze an Excel sales file and contrasts that approach with local filesystem tools.

Key terms: code_execution="e2b", execute_python, bash_tool, upload_file, pandas, filesystem tools, sandbox, pip install, state

8.5 Agent skills

This section explains why a growing tool catalog strains an agent’s prompt and introduces agent skills as a way to reduce that burden.

Key terms: MCP server, tool definitions, prompt, tokens, tool proliferation, agent skills, progressively disclosing tool information, tool portability, folder-based structure

8.5.1 The agent skills concept

This section explains how agent skills package instructions and optional code in portable folders while giving the LLM brief descriptions before it needs the full documentation.

Key terms: agent skills, YAML front matter, SKILL.md, skill folder, scripts, tool-portability problem

8.5.2 Progressive tool-information disclosure

This section explains how agent skills reveal tool information only as needed, reducing the tokens used for most requests.

Key terms: progressively disclosing tool information, skill list, SKILL.md, bash_tool, traditional approach, agent skills approach, tokens

8.5.3 Implementation

This section shows how to discover agent skills, summarize them in the system prompt, and place their full files in the agent’s sandbox.

Key terms: system prompt, skill list, skill directories, SkillInfo, SKILL.md, YAML front matter, parse_frontmatter, load_skill, discover_skills, skills_path

8.5.4 Practical example: PDF merge skill

This section shows how a PDF-merge skill is registered with an agent and used only when a request calls for it.

Key terms: pdf-merge, SKILL.md, pypdf, skill folder path, one-line description, skill list, system prompt, bash_tool, execute_python, merged.pdf

8.5.5 Hierarchical tool structure

This section organizes agent tools into three layers and explains how a fixed interface provides broad capabilities without putting every tool definition in the prompt.

Key terms: core tools, installable tools, agent skills, generated tools, bash_tool, execute_python, hierarchical structure, workspace, tool definitions, tool summaries

8.6 Summary

The chapter summary explains how code execution expands agent actions, why it needs a sandbox and workspace, and how layered tools keep prompts lean.

Concepts explained in this chapter

← Chapter 7: Planning and reflection for complex tasks Chapter 9: Orchestrating multi-agent systems →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.