Welcome back! In Chapter 2: Data Contract (Schemas), we built strict "Customs Declaration Forms" (Schemas) so our AI knows syntax: what data format to send and receive.
But knowing syntax isn't the same as knowing strategy.
Imagine you hire a brilliant research assistant, but they have been living in a cave since 2021. You ask them: "Find the latest iPhone reviews."
To fix this, we need to give the AI a Mission Briefing before it starts working. In the world of AI, this is called Prompt Engineering Context.
This abstraction solves three specific problems that code schemas cannot solve:
Think of the Prompt Engineering Context as the set of standing orders given to a soldier. It tells them not just how to fire the weapon (Schema), but when to fire and what rules to follow.
AI models are trained on static data. If you ask an AI "What is the current year?", it might hallucinate or give you its training cutoff date.
To use a Web Search tool effectively, the AI needs to know the current date so it can search for "Best Laptops 2025" instead of just "Best Laptops."
We handle this in a file called prompt.ts. We use a helper function to get the real-world date and inject it directly into the instructions.
// prompt.ts
import { getLocalMonthYear } from 'src/constants/common.js'
export function getWebSearchPrompt(): string {
// 1. Get the real time from the system
const currentMonthYear = getLocalMonthYear()
// 2. Inject it into the text
return `... The current month is ${currentMonthYear} ...`
}
Explanation:
By using the ${currentMonthYear} variable, we dynamically update the instructions every time the tool is used. The AI reads this and thinks: "Ah, I am operating in March 2025. I should adjust my search queries accordingly."
If an AI browses the web and just gives you an answer, you have no way of verifying it. We want the AI to act like a journalist: act on facts and link to sources.
We don't leave this to chance. We write a Critical Requirement in the prompt.
// prompt.ts
/* ... inside the string return ... */
`
CRITICAL REQUIREMENT - You MUST follow this:
- After answering, you MUST include a "Sources:" section
- List all relevant URLs as markdown hyperlinks
- Example format:
[Your answer here]
Sources:
- [Title](https://example.com/1)
`
Explanation: We use capitalization ("CRITICAL REQUIREMENT", "MUST") to stress importance. LLMs (Large Language Models) pay closer attention to these "system instructions" than to normal user chat. This ensures the output is verifiable.
Now that we have written the "Mission Briefing," how does the WebSearchTool actually use it?
Recall the buildTool function from Chapter 1: Tool Definition & Lifecycle. There is a specific property called prompt.
// WebSearchTool.ts
import { getWebSearchPrompt } from './prompt.js'
export const WebSearchTool = buildTool({
name: 'web_search',
// ... other properties ...
// The tool provides its own instructions
async prompt() {
return getWebSearchPrompt()
},
})
Explanation:
When the AI system starts up, it asks every tool: "Do you have any special instructions for the model?" The WebSearchTool responds by running getWebSearchPrompt(), generating the text with the current date, and handing it over.
Here is how this context gets from our code into the "brain" of the AI.
Here is what the final generated prompt looks like. This is exactly what the AI reads before it processes your request.
System Instruction:
* Allows Claude to search the web...
* IMPORTANT - Use the correct year: The current month is March 2025. You MUST use this year when searching for recent information.
* CRITICAL REQUIREMENT: You MUST include a "Sources:" section at the end of your response...
By combining the Structure (Chapter 2) with this Context (Chapter 3), the AI is now fully equipped.
We have successfully briefed our "AI Contractor." It knows the date, it knows to cite its sources, and it knows the shape of the data to send.
But we haven't actually sent the AI to the internet yet. We have just prepared it.
In the next chapter, we will tackle the most complex part of the system: Execution. We will look at how the tool actually connects to the browser, streams data, and handles the back-and-forth conversation with the web.
Next Chapter: Streaming Execution Strategy
Generated by Code IQ