Welcome back! In the previous chapter, Chapter 3: Teammate Approval Protocol, we added a layer of bureaucracy to ensure plans are approved by a human or a team lead.
We have built the Tool (the code), the Logic (the state transition), and the Safety (the approval protocol). But we are missing one critical piece: Instruction.
Just because a tool exists doesn't mean the AI knows when or how to use it. We need to teach it.
Imagine you are a movie director. You hand an actor a script that says:
Action: Walk through the door.
If you don't give context, the actor might walk through the door while the cameras are off, or while the set is still being built. You need to give them Director's Notes:
"Only walk through this door when the scene is set, the lights are on, and you are ready to face the villain. Do not walk through it if you are just rehearsing."
In AI development, these notes are called Prompt Engineering. We write natural language instructions to tell the AI the "motivation" behind the tool.
The ExitPlanModeTool is a signal. It signifies that the "Thinking Phase" is over and the "Coding Phase" is beginning.
If we don't explain this clearly, the AI might get confused. It might try to use this tool when:
We need to set strict boundaries.
A good tool prompt usually has three parts:
Let's look at how we build this string in our code.
First, we tell the AI exactly what the tool expects.
// Inside prompt.ts
export const EXIT_PLAN_MODE_V2_TOOL_PROMPT = `
Use this tool when you are in plan mode and have finished writing your plan.
## How This Tool Works
- You should have already written your plan to the plan file
- This tool does NOT take the plan content as a parameter
- It signals that you're done planning and ready for review
`
This is the most important instruction. We must distinguish between Research (looking around) and Implementation (building things).
// Continued in prompt.ts...
`
## When to Use This Tool
IMPORTANT: Only use this tool when the task requires planning implementation steps.
For research tasks where you're gathering information, searching files,
or trying to understand the codebase - do NOT use this tool.
`
A common mistake AI models make is being too polite. They might try to use a generic "Ask Question" tool to say, "I have finished my plan, is it okay?"
We need to tell the AI that ExitPlanModeTool IS the question.
// Continued in prompt.ts...
`
## Before Using This Tool
Ensure your plan is complete and unambiguous.
**Important:** Do NOT use AskUserQuestion to ask "Is this plan okay?"
That is exactly what THIS tool does.
ExitPlanMode inherently requests user approval.
`
When the AI reads these instructions, it forms a decision tree. Here is what we want the AI's internal thought process to look like:
The most effective way to teach an AI is by showing, not just telling. We provide specific examples of user inputs and whether the tool should be used.
// Continued in prompt.ts...
`
## Examples
1. Initial task: "Search for and understand the vim mode implementation"
- Do not use the exit plan mode tool.
- Reason: You are not planning implementation steps.
`
// Continued in prompt.ts...
`
2. Initial task: "Help me implement yank mode for vim"
- Use the exit plan mode tool.
- Reason: You have finished planning the implementation steps.
`
In our codebase, we don't hardcode this massive string inside the tool definition file (Chapter 1). That would make the code hard to read.
Instead, we keep prompts in a dedicated file (prompt.ts) and import them.
// ExitPlanModeV2Tool.ts (The Definition)
import { EXIT_PLAN_MODE_V2_TOOL_PROMPT } from './prompt.js'
export const ExitPlanModeV2Tool = buildTool({
name: 'exit_plan_mode_v2',
// We inject the instructions into the description
description: async () => EXIT_PLAN_MODE_V2_TOOL_PROMPT,
// ... rest of the tool definition
})
In this chapter, we learned that code logic isn't enough. We must use Prompt Engineering to give the AI the context it needs to behave correctly.
Now the Agent knows how (Code), when (State), and why (Prompt) to use the tool.
The final piece of the puzzle is the human user. When the Agent exits plan mode, how does the user see what is happening? It shouldn't just be a wall of text.
Next Chapter: CLI UI Rendering
Generated by Code IQ