There Is No Application · Chapter 7
The Art of the Prompt: Engineering Effective AI Instructions
In the last chapter, we built an agent whose core competence was powered by a LangChain prompt. That prompt was the instruction we gave to the LLM, the "brain" of our agent. It's time to zoom in on this critical skill: prompt engineering.
If you think of the LLM as a brilliant, talented, but very literal junior developer, then the prompt is your project brief. A vague brief will get you a vague and unhelpful result. A clear, precise, and well-structured brief will get you exactly what you need, quickly and reliably. In the world of AI agents, the prompt is your primary API for interacting with intelligence.
Mastering the art of the prompt is arguably the most important leverage point you have as an AI developer.
The Anatomy of a Great Prompt
While prompts can vary wildly, the most effective ones—especially for building autonomous agents—share a common structure. Think of it as a checklist to ensure you're giving the LLM everything it needs to succeed.
- Role & Goal: Start by giving the model a persona and a clear objective. This focuses its attention and sets the context.
- Bad: "Summarize the text."
- Good: "You are an expert financial analyst. Your goal is to summarize the following quarterly report for a busy executive, focusing only on revenue, profit, and future guidance."
- The Task: Clearly state the action you want the model to perform.
- Bad: "Look at this data."
- Good: "Extract the name, email address, and company from the following block of text."
- The Context: Provide the raw material the LLM will work on. This is usually where you insert your input variables, like
{document}or{user_question}. - The Output Format: This is the most critical part for building reliable agents. You must be relentlessly explicit about the format you expect in return. If you need JSON, specify the exact schema.
- Bad: "Tell me the key points."
- Good:
Provide your response as a JSON object with the following structure: { "key_points": ["Point 1", "Point 2"] }
Putting it all together, a great prompt template looks like a detailed set of instructions you'd give to a human.
Technique 1: Few-Shot Prompting (Learning by Example)
Sometimes, just describing the output format isn't enough. The best way to show the model what you want is to give it a few examples. This is called few-shot prompting.
Let's say we want to build a sentiment analysis agent.
A zero-shot prompt (no examples) might be:
> Classify the sentiment of the following text as "Positive", "Negative", or "Neutral".
>
> Text: {text_input}
>
> Sentiment:
This might work, but it can be unreliable. A few-shot prompt is far more robust:
> You are a sentiment classification expert. Your goal is to classify text as "Positive", "Negative", or "Neutral".
>
> Text: "I love the new design, it's so intuitive!"
> Sentiment: "Positive"
>
> Text: "The checkout process was confusing and took forever."
> Sentiment: "Negative"
>
> Text: "The package arrived today."
> Sentiment: "Neutral"
>
> Text: {text_input}
> Sentiment:
By providing examples, you are giving the model a clear pattern to follow. It dramatically increases the chances that the output will be correct and properly formatted, especially for classification, extraction, and formatting tasks.
Technique 2: Chain-of-Thought Prompting (Thinking Step-by-Step)
LLMs can sometimes rush to an answer, especially for problems that require logical steps. They might see a pattern and jump to a conclusion, even if it's incorrect. To combat this, we can use Chain-of-Thought (CoT) prompting.
The technique is simple: you explicitly instruct the model to "think step by step" before giving its final answer.
Imagine you need to extract the total cost from an invoice that includes line items and a sales tax.
A simple prompt might fail if the logic is tricky. A CoT prompt would look like this:
> You are an accounting assistant. Your task is to calculate the total cost from the following invoice. First, think step-by-step about how you will calculate the total. Then, provide the final answer as a JSON object.
>
> Invoice:
> Item A: $10.00
> Item B: $25.00
> Sales Tax: 8%
>
> Your Thought Process:
> 1. First, I need to sum the price of all line items.
> 2. Item A is $10.00 and Item B is $25.00, so the subtotal is $35.00.
> 3. Next, I need to calculate the sales tax. 8% of $35.00 is $2.80.
> 4. Finally, I need to add the subtotal and the sales tax to get the total cost. $35.00 + $2.80 is $37.80.
>
> Final Answer:
> ```json
> {
> "total_cost": 37.80
> }
> ```
By forcing the model to articulate its reasoning process, you make it far more likely to arrive at the correct answer. This is an incredibly powerful technique for any task that involves logic, calculation, or multi-step reasoning.
Building a Reusable Prompt Library
As your system grows, you'll find you're writing similar prompts over and over. Just like functions or components, prompts are reusable assets that should be managed as part of your codebase.
A great practice is to create a src/prompts.ts file to store your PromptTemplate objects.
```typescript
// src/prompts.ts
import { PromptTemplate } from "@langchain/core/prompts";
export const summarizationTemplate = new PromptTemplate({
template: `
You are a world-class summarization expert.
Summarize the following document for a busy executive.
Document: {document}
Summary:
`,
inputVariables: ["document"],
});
export const jsonExtractionTemplate = new PromptTemplate({
template: `
You are a data extraction specialist. Your goal is to extract specific entities from the provided text and return them as a JSON object.
Text: {text_input}
Extract the name and email address and provide your response as a JSON object with the following structure:
{{
"name": "The extracted name",
"email": "The extracted email"
}}
`,
inputVariables: ["text_input"],
});
```
Now, in your agent's code, you can simply import the template you need:
```typescript
import { summarizationTemplate } from './prompts';
// ...
const chain = new LLMChain({ llm: model, prompt: summarizationTemplate });
```
This keeps your main logic clean, prevents you from duplicating prompts, and makes your entire system much easier to maintain and update.
Iteration is Key
Prompt engineering is not a one-shot process. It's a cycle of writing, testing, and refining. Your first prompt will rarely be your best. The key is to start simple, see where the model fails or produces unreliable output, and then use the techniques in this chapter to improve it. Is the formatting wrong? Add a few-shot example. Is the logic incorrect? Add a chain-of-thought instruction.
By treating your prompts as a core part of your application's code and iteratively improving them, you will unlock the true potential of your AI agents.
