Prompt Engineering: Clear Instructions for Models

Prompt engineering is mostly specification writing. State the task, provide the context the model cannot know, show the shape of a good answer and say what it must not do.

A prompt that is easy to test

I normally start with four parts. I add only the ones the task needs.

  • Role: Tell the AI who it should be. "You are a helpful and funny pirate."
  • Task: Tell the AI what to do. "Explain TypeScript generics."
  • Context: Give it relevant background information. "The user is a beginner programmer who only knows JavaScript."
  • Examples (Few-shot): Show the AI exactly what you want. This is one of the most reliable techniques.

The examples below show the progression from a simple call to a constrained application prompt.

Basic Prompting with generateText

The simplest way to interact with a model is to send a straightforward prompt. The Vercel AI SDK's generateText function handles this.

// lib/prompts/basic-prompt.ts
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import 'dotenv/config';

async function main() {
  const { text } = await generateText({
    model: openai('gpt-4o'),
    prompt: 'Why is the sky blue?',
  });

  console.log(text);
}

main();

This is a "zero-shot" prompt. It provides no examples and relies entirely on the model's pre-existing knowledge. It works well for general questions, but more specific tasks need additional context.

Technique 1: Assigning a Role

One of the easiest ways to improve a response is to give the AI a persona. This helps guide its tone, style, and even the kind of information it provides.

// lib/prompts/role-prompt.ts
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import 'dotenv/config';

async function main() {
  const { text } = await generateText({
    model: openai('gpt-4o'),
    // By giving the model a role, we influence its response style.
    system: 'You are a sarcastic, world-weary poet.',
    prompt: 'Write a short poem about the challenges of modern software development.',
  });

  console.log(text);
}

main();

The system parameter sets the stage for the entire conversation, telling the model what character it should play.

Technique 2: Providing Examples (Few-Shot Learning)

"Few-shot" prompting means putting a few input/output examples in the prompt. It is useful when the format matters and a prose instruction is not precise enough.

Here is a small sentiment analyser.

// lib/prompts/few-shot-prompt.ts
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import 'dotenv/config';

async function main() {
  const { text } = await generateText({
    model: openai('gpt-4o'),
    prompt: `Analyze the sentiment of the following reviews and respond with 'Positive', 'Negative', or 'Neutral'.

Review: "I absolutely love this new coffee machine! It's fast and makes the perfect cup every time."
Sentiment: Positive

Review: "The product arrived broken and customer service was unhelpful."
Sentiment: Negative

Review: "The shipping was on time."
Sentiment: Neutral

Review: "This is the best purchase I've made all year, I can't recommend it enough!"
Sentiment:`,
  });

  console.log(text); // Expected: Positive
}

main();

By showing it examples, I have taught the model the task and the desired output format without adding complex code.

Technique 3: Chain-of-Thought Prompting

For complex problems that require multiple steps of reasoning, you can ask the model to "think step-by-step." This is called Chain-of-Thought (CoT) prompting. It forces the model to break down the problem, which often leads to more accurate results.

// lib/prompts/cot-prompt.ts
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import 'dotenv/config';

async function main() {
  const { text } = await generateText({
    model: openai('gpt-4o'),
    prompt: `A grocery store has 15 apples. They receive a shipment of 3 crates, each containing 24 apples.
If they sell 52 apples in one day, how many apples are left?

The reasoning can be broken down step by step:
1.  Start with the initial number of apples.
2.  Calculate the total number of apples received in the shipment.
3.  Add the new apples to the initial stock.
4.  Subtract the number of apples sold.
5.  State the final number of apples remaining.

Here is the step-by-step solution:
`,
  });

  console.log(text);
}

main();

By outlining the steps, we guide the model's reasoning process, making it less likely to make a simple arithmetic mistake.

Putting It All Together: A Practical Example

The following prompt generates a product description by combining a role, context and specific constraints.

// lib/prompts/product-description-prompt.ts
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import 'dotenv/config';

async function generateProductDescription(productName: string, features: string[], targetAudience: string) {
  const { text } = await generateText({
    model: openai('gpt-4o'),
    system: "You are an expert copywriter for a high-end electronics brand. You write in a clear, confident, and slightly minimalist tone.",
    prompt: `Generate a product description for the following product.

Product Name: ${productName}

Key Features:
- ${features.join('\n- ')}

Target Audience: ${targetAudience}

Constraints:
- The description must be under 120 words.
- Do not use exclamation points or cheesy marketing jargon.
- End with a simple, compelling call to action.
`,
  });
  return text;
}

async function main() {
  const description = await generateProductDescription(
    'Aura Headphones',
    ['Active Noise Cancellation', '30-hour battery life', 'Crystal-clear microphone', 'All-day comfort design'],
    'Remote workers and frequent travelers'
  );

  console.log(description);
}

main();

The useful part is not the persona. It is the explicit input fields, length limit and output constraints.

What I check before shipping

  1. Be Specific, Not Vague: Instead of "Write about our product," say "Write a 3-paragraph blog post explaining how our product's 'Auto-Sync' feature helps busy professionals save time."

  2. Iterate and Experiment: Your first prompt is rarely your best. Try different phrasings, add or remove examples, and see what works best.

  3. Adjust the temperature: For more creative tasks (like writing a poem), you can increase the temperature to get more varied results. For factual tasks, keep it low (e.g., 0.2) to get more deterministic outputs.

  4. Use a concrete task. Replace “write about our product” with the audience, input and required result.

  5. Keep examples realistic. Examples should represent the cases the application will actually receive.

  6. Constrain the output. Use structured output when another part of the system will parse the response.

  7. Measure the change. Keep a small test set so prompt edits can be compared instead of judged by one lucky response.

The goal is not clever wording. It is a prompt that produces a useful result often enough to be part of a product.