Overview

4 Agent: Stop telling AI what to read. Let it decide.

AI agents are most valuable when they are used for focused exploration rather than broad codebase mapping. A workflow can spend separate, fresh context on navigation, comprehension, and explanation, but an agent must use one shared context window for all three. That makes it easy for an agent to skim too many files, build a shallow list of names and signatures, and miss the real purpose of the system. The chapter’s core lesson is that asking an agent to do a workflow’s job wastes its strengths; agents work best when they are given a narrow question and allowed to decide what to read next.

When the prompt is specific, the agent can trace a single behavior across many files and build a much better mental model. The chapter shows this with the path from clicking a file in VS Code’s Explorer to seeing text appear in the editor: the agent follows calls through the explorer UI, editor service, editor groups, file loading, and Monaco rendering, recovering from dead ends and connecting the layers into one coherent flow. This kind of trace reveals relationships that no single file exposes, such as multiple entry points converging on the same gateway or the split between raw byte reading and text decoding. The result is an actionable understanding of how the system actually moves from user action to rendered output.

The chapter then pushes understanding further by having the agent write tests and build the project itself. Understanding tests check whether a mental model matches actual runtime behavior, and the URI example shows how a test can expose surprising ordering, like path cleanup happening before validation. A build-and-run task demonstrates that agents are also useful for tedious setup work, since they can read errors, fix environment mismatches, and keep going without frustration. Finally, the chapter recommends preserving what the agent learns in a persistent skill file: one file for codebase-specific facts and another for reusable general lessons. That way, each session starts with accumulated knowledge instead of repeating the same discovery work from scratch.

The agent traces, tests, and runs code to learn the codebase, saving what it learns in a skill file so the next session can build on that knowledge instead of starting from scratch.
A workflow reads one thing deeply to understand it (left). An agent tries to do everything at once (right): read file A, search for B, and reason about C. The difference is focused work versus overwhelmed multitasking.
A pipeline gives each step a fresh context window. An agent uses one window for everything (reading files, calling tools, and its own reasoning), so it quickly runs out of space, skims the first 80 lines of a 2,000-line file, and moves on.
The agent read a validator that promised to throw on a bad path and was sure it would fail. But running the code proved it wrong: a silent cleanup step fixed the path before the validator ran, so the test passed instead of erroring.
The agent fixes a common Node.js setup failure in four minutes. When compilation errors point to a version mismatch, it reads the .nvmrc file, switches Node versions, retries the build, and launches VS Code.
The agent generates two skill files: one with knowledge specific to the current codebase, and another with general techniques that apply to any codebase.

Summary

  • Ask the agent one focused question at a time. A broad prompt forces the agent to spread its limited context window thin across dozens of files. A focused prompt lets it spend that same budget finding connections between files, the kind of insight you can't get from just reading the code yourself.
  • Turn your beliefs into evidence with "understanding tests." Write a small test that asserts something specific about the system's behavior, then run it. For example, a single failing test on URI.from() proved that path cleanup runs before validation, an ordering that reading the source code alone doesn't reveal. A failing test is more valuable than a dozen passing ones because it corrects your mental model, not the code.
  • Tests confirm behavior, but only a running system reveals hidden constraints. Let the agent deal with the yak-shaving of Node versions, native modules, and missing build tools so you can get straight to observing how the system works. Every build error is a constraint the docs forgot to mention.
  • Your understanding only compounds if you write it down. At the end of each session, have the agent save what it learned to the skill file: build commands, gotchas, and important file locations. The next day's focused question can then start from today's knowledge instead of from scratch.
  • Chat, workflow, and agent form a single loop. Run a workflow once to map the codebase (chapter 3). Use the agent daily to investigate specific areas with focused questions. Use chat for quick lookups mid-session. The skill file acts as the bridge between all three modes, so your comprehension debt shrinks instead of growing.

FAQ

Why is asking an AI agent to “map the whole codebase” usually a bad prompt?An agent has one shared context window for reading, reasoning, and tool use. Broad prompts make it skim many files, spend tokens on navigation, and return shallow summaries like class signatures instead of real understanding. It works best when the question is narrow and deep.
What is the main difference between a workflow and an agent?A workflow follows a fixed pipeline, and each step starts fresh with its own context. An agent runs in a decide-act-observe loop, using the same context window across multiple turns, which makes it flexible but also prone to context exhaustion.
Why do agents struggle with broad questions but do better with specific ones?Broad questions force the agent to spread its context across many files and many possible paths. Specific questions let it spend its budget on one line of inquiry, which produces deeper traces, clearer explanations, and more useful answers.
What kind of question is an agent especially good at answering?Agents are best at “long tail” questions that are specific and unpredictable, such as tracing a user action through the codebase, understanding a weird bug, or explaining why a particular constructor or service behaves the way it does.
What did the file-opening trace example show?It showed how a single user action, clicking a file in VS Code, travels through multiple layers: the explorer UI, the editor service gateway, editor grouping, file loading, and finally the Monaco editor. The trace revealed that different entry points all converge on the same open-editor path.
Why does tracing a flow across files help more than reading one file deeply?A trace exposes how separate parts of the system connect, which one file cannot show by itself. It gives you a concrete mental model of the pipeline, helping you understand where control starts, where it branches, and where to look when something breaks.
What is an understanding test?An understanding test is a test written to verify one specific belief about how the code works. It is not for coverage; it is for checking whether your mental model matches actual runtime behavior.
What did the URI test reveal about cleanup and validation?It showed that VS Code first cleans up some URI paths before validating them. That means a bad path may be silently fixed before a validator sees it, so a test expecting an error can fail because the cleanup step runs first.
Why is building and running the codebase valuable for understanding it?Running the system shows ground truth that static reading cannot. You can see real processes, extension activation order, runtime boundaries, and actual behavior under the environment, which often exposes issues or architecture details you would miss in code alone.
Why should the agent write to a skill file at the end of a session?A skill file preserves what the agent learned so future sessions do not start from zero. Codebase-specific facts go in the repo-local file, while reusable exploration rules go in the global file, letting knowledge compound across sessions and projects.

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
  • Crack Any Codebase with AI 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
  • Crack Any Codebase with AI ebook for free