In the previous chapter, Input Validation Schema, we built a "bouncer" to ensure only valid data enters our tool.
We now have a tool that works (Logic), is safe (Validation), and has a name (Definition). But there is one problem: The AI doesn't know when to use it.
Welcome to Prompt Strategy.
Imagine you hire a new employee. You give them a powerful industrial shredder (the Tool). If you don't give them clear instructions, they might shred important contracts thinking they are helping clean up.
You need Standing Orders:
For an AI, the Prompt Strategy is this set of standing orders. It prevents the AI from using the "heavy duty" Worktree tool when a simple Git command would suffice.
The Scenario: A user says, "Fix the typo in the README."
The Risk: The AI sees EnterWorktreeTool and thinks, "I should create a whole new isolated environment just for this typo!" This is overkill and annoying for the user.
The Solution: We write a prompt that tells the AI: "Use this tool ONLY when the user explicitly asks for a 'worktree'."
We communicate with the AI using natural language. We structure this language into three specific categories:
These are the strict conditions required to activate the tool. We usually look for specific keywords in the user's request.
This is a list of negative constraints. We explicitly list scenarios where the AI might be tempted to use the tool but shouldn't.
We explain to the AI what the tool does to the file system (e.g., "switches the directory"). This helps the AI predict the outcome and update its mental model of the session.
Let's look at how we write these instructions in the prompt.ts file.
We start by setting a very high bar for entry.
export function getEnterWorktreeToolPrompt(): string {
return `Use this tool ONLY when the user explicitly asks to work in a worktree.
## When to Use
- The user explicitly says "worktree"
- Examples: "start a worktree", "create a worktree"
`
// ... continued
}
Explanation: We use capitalization ("ONLY") to emphasize strictness. We give concrete examples of what the user must say.
Next, we list the "Anti-Patterns."
/* ... inside the string ... */
`
## When NOT to Use
- The user asks to create/switch branches -> use git commands
- The user asks to fix a bug -> use normal git workflow
- Never use this unless "worktree" is mentioned
`
Explanation: The AI knows standard Git. We must tell it: "Standard Git is the default. This tool is the exception."
Finally, we tell the AI what physically happens.
/* ... inside the string ... */
`
## Behavior
- Creates a new git worktree inside .claude/worktrees/
- Switches the session's working directory to the new worktree
- Use ExitWorktree to leave the worktree mid-session
`
Explanation: By saying "Switches the session's working directory," the AI understands that after this tool runs, it will be looking at a different folder.
How does the system use this text? It injects it into the AI's "context window" just before it decides which tool to use.
In our tool definition file (EnterWorktreeTool.ts), we connect this text to the tool object.
import { getEnterWorktreeToolPrompt } from './prompt.js'
export const EnterWorktreeTool = buildTool({
// ... other settings
// This method provides the strategy to the AI
async prompt() {
return getEnterWorktreeToolPrompt()
},
// ... call function
})
Explanation: The buildTool function expects a prompt() method. We simply return the string we crafted in the helper file.
description?
In Tool Definition, we defined a description.
In this chapter, we learned that code isn't enough; we need to teach the AI how to behave.
Now the AI knows when to use the tool, how to validate inputs, and what logic to run. The final piece of the puzzle is the human user. How do we show the user what is happening?
Generated by Code IQ