AI Agent Guide Interactive
Part 1 · Prompt Engineering 4 Interactive Labs TypeScript / Anthropic SDK

Prompt Engineering: From Asking to Constraining Models

Most developers start out thinking prompt engineering is "how to talk to an AI"—until production hits and a crystal-clear prompt still produces flaky outputs. At its core, a prompt is not a conversation with a model; it is structured context that narrows the next-token probability distribution into your desired boundary.

30-Second Summary · 3 Engineering Takeaways
  1. 01 Know whose job it is: enforce hard formats with Schemas, ground external facts with RAG, and use prompts to steer phrasing and boundaries.
  2. 02 High temperature amplifies low-probability rule-breaking tokens; in long prompts, place critical format constraints at the start and end so they don't fade in the middle.
  3. 03 Use edge-case few-shot examples for sarcasm and tricky classification; always isolate user input inside prompt templates to prevent fence-escape injection.
Fig 1 · Under the Hood: Prompts Narrow the Next-Token Distribution Concept Diagram
Vague Free-Form Ask "Review this code for me" Wide Distribution No hard boundaries • Long tutorial essay (unparseable) • Rewrites the entire file (off-spec) • Sometimes JSON, sometimes Markdown (flaky) Structured Context Role + Rules + Few-shot + Start/End Format Rules Narrowed Mass Locks target region ✓ Lands Inside Expected Output Bounds Controllable format, consistent style, ready to validate

A prompt isn't negotiating with the model—it compresses the sampling space to squeeze out invalid candidates.

Diagnose Before You Prompt: What Prompts Cannot Fix

When an LLM output breaks in production, resist the urge to pile more "NEVER do X" lines into the prompt. Pick one of the 4 real incidents below and test your first instinct.

Interactive Clinic · Capability Boundaries

Prompt Triage Clinic: Who Owns This Failure?

Select a production incident, then pick your first-line fix to see what actually happens.

Choose your fix:
Fig 2 · Engineering Triage Flow for LLM Output Failures Architecture Routing
Output Failure Identify root cause Tone, style, reasoning, or edge examples? ★ Prompt Engineering (This Chapter) Missing private docs or fresh business data? RAG (Retrieval-Augmented Gen) Must JSON parse 100% of the time? Zod / JSON Schema Guardrails Tool calls, memory, or long-turn decay? Context Engineering / Agent Loop Model upgrade caused behavior drift? Automated Prompt Regression Evals

The Google & Kaggle Prompt Engineering Whitepaper maps out core techniques including sampling, few-shot prompting, CoT, and ReAct. Confirming that a problem actually belongs to the prompt layer first keeps you from patching architecture gaps with endless prompt prose.

Why the Same Prompt Returns Different Outputs

Even without changing a single character, running the same prompt 5 times can produce 5 different outputs—and even break your format rules. Make a prediction first, then crank up Temperature in the dice machine below.

Predict First You ask a model to name a pair of headphones in "1–2 short words, max 10 characters, no punctuation." You run it 5 times at temperature: 0 and 5 times at temperature: 1. What happens?
Spot on. Push Temperature above 1.0 in the lab below and watch what happens: low-probability rule-breaking tokens (like trailing punctuation or oversized suffixes) gain enough weight to slip through and violate your format constraints.
Interactive Lab 01 · Token Sampling Dice

Sampling Dice: Raise Temperature and Watch Constraints Break

Adjust the sliders and click "Sample 5 Runs" to see how red rule-breaking tokens creep into the output at higher temperatures (simulated distribution for intuition).

Task: Name a pair of headphones with studio-grade sound but only 3 hours of battery life.
System Constraint: Output one product name only, max 10 characters, no punctuation, no explanation, no list.
Temperature 0.0
Top-K Cutoff 7
Top-P (Nucleus) 1.0
Candidate Token Distribution (red = rule violation)
Sampled Outputs (5 runs)
View TypeScript Source: 01-temperature.ts (synced with sliders)
examples/ai-coding/prompt-engineering/01-temperature.ts
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

async function generate(temperature: number) {
  return client.messages.create({
    model: process.env.MODEL!,
    max_tokens: 50,
    temperature,
    system: "Output one product name only, max 10 characters, no punctuation, no explanation, no list.",
    messages: [{ role: "user", content: "Name a pair of headphones: studio-grade sound, only 3 hours of battery." }],
  });
}

// Current lab parameter: temperature = 0.0
const response = await generate(0.0);
const textBlock = response.content.find((b) => b.type === "text");
console.log(textBlock?.type === "text" ? textBlock.text : "");

When predicting the next token, an LLM samples from a vocabulary probability distribution: Temperature sharpens or flattens the curve, Top-K keeps only the top K candidates, and Top-P (nucleus sampling) keeps the smallest prefix whose cumulative probability reaches P. In engineering workflows, start at temperature: 0 for classification, extraction, and structured output, and only raise it to 0.7–1.0 for brainstorming; leave Top-K and Top-P at their defaults in most cases.

Engineering Note · Thinking Mode & Sampling Parameters
When reasoning models such as DeepSeek-V4-Pro run with thinking mode enabled, sampling parameters like temperature and top_p are ignored. To isolate temperature effects, explicitly disable thinking mode (thinking: { type: "disabled" })—see the DeepSeek Thinking Mode docs for exact behavior.

Structured Prompts and Position Effects

In a long prompt, where you place a rule matters as much as what it says. Use the ↑ / ↓ buttons below to bury [Core Format Rule] in the middle of a 3,000-word spec and watch the simulated JSON.parse() fail.

Interactive Lab 02 · Block Assembler & Lost in the Middle

Prompt Block Assembler: Move a Rule, Watch Parsing Break or Recover

Reorder the blocks with ↑ / ↓ or toggle "Re-state format at the end" to see the U-shaped attention curve and simulated JSON parsing status.

Reorder Prompt Blocks (click ↑ / ↓)
Attention U-Curve & Parse Result (Simulated) Inspired by Liu et al., 2023
Rule Attention Weight
--
Simulated JSON Parse
--

As the U-curve illustrates, Stanford and Meta AI researchers showed in Lost in the Middle Source: Liu et al., 2023 ↗ that LLMs pay the highest attention to the beginning and end of long contexts while diluting information in the middle. In practice, separate your prompt into a System Prompt (role, rules, and output contract) and a User Message (dynamic input), combine System / Role / Contextual Prompting, and anchor critical format rules at the start and end.

From Zero-Shot to Few-Shot and Chain of Thought

Sarcastic reviews like "Awesome, charged it all night and it's still at 0%" routinely trip up zero-shot classifiers. Toggle the examples below to see what fixes it, then switch to Tab 2 to compare reasoning output channels.

Interactive Lab 03 · Few-shot Builder & CoT

Few-Shot Example Builder & CoT vs. Native Thinking

Select which examples to include in the prompt and test when a sarcastic review gets classified accurately vs. misclassified (simulated output).

1. Select Examples to Inject
Test Input: "Awesome, charged it all night and it's still at 0%."
2. Live Classification Result (Simulated)
View TypeScript Source: 02-few-shot.ts
examples/ai-coding/prompt-engineering/02-few-shot.ts
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const prompt = `Classify the user review as: Positive, Neutral, or Negative.

Example:
Review: "Great sound quality and solid value for the price."
Label: Positive

Example:
Review: "Pretty average overall, nothing stands out."
Label: Neutral

Example:
Review: "Waited two weeks and it arrived dead out of the box—what a treat."
Label: Negative

Now classify:
Review: "Great sound on these headphones, but the battery life is terrible."
Label:`;

const response = await client.messages.create({
  model: process.env.MODEL!,
  max_tokens: 20,
  temperature: 0,
  messages: [{ role: "user", content: prompt }],
});
Test Puzzle: "When I was 3, my partner was 3 times my age. Now I'm 20—how old is my partner?"
1. Direct Answer ~12 Tokens
60 years old. (Misled by "3 times", blindly computes 20 × 3)
Lowest latency, but error-prone on multi-step logic traps—and impossible to audit where it went wrong.
2. Prompt-Guided CoT ~78 Tokens
1. When I was 3, partner was 3 × 3 = 9 2. Age gap is 9 - 3 = 6 years 3. At age 20, partner is 20 + 6 = 26 Final answer: 26
Reasoning appears inline in the text body, billed as output tokens, and easy for engineers to inspect.
3. Native Thinking Mode Dual Blocks
[thinking / reasoning_content block] At age 3, partner was 3×3=9; age gap stays constant at 6. 20+6=26.
[text block] 26.
Always extract with content.find(b => b.type === "text")—never hardcode content[0].
Fig 3 · Output Channel Differences: Prompt CoT vs. Native Thinking Mode Architecture Comparison
Prompt CoT Technique Append "Think step by step" LLM Inference Single Text Stream [Steps + Final Answer] mixed together, billed as output Native Thinking Mode State task & acceptance rules Built-in Thinking Dual-channel Block 0: thinking / reasoning_content (isolated trace) Block 1: text (final answer — read via find(b => b.type === "text"))

With few-shot prompting, quality and edge-case coverage beat quantity: shuffle class order in classification prompts and deliberately include 1–2 sarcastic or borderline examples. For multi-step reasoning, CoT (Wei et al., 2022, Kojima et al., 2022) surfaces intermediate steps in the text body, while native thinking modes (such as DeepSeek's reasoning_content or Claude's thinking blocks—see Claude Extended Thinking docs) return a dedicated reasoning block, so you don't need to mechanically append "think step by step."

Positive Instructions, Advanced Patterns, and Template Security

How risky is interpolating raw user input into a prompt template function? Click the malicious userCode payload below to see how a code-review bot gets hijacked—then toggle the defense to block it.

Interactive Lab 04 · Prompt Injection Sandbox

Red vs. Blue: Break Your Own buildReviewPrompt

Pick a userCode snippet sent to a code-review assistant and see how unescaped input closes the Markdown fence and hijacks the output (simulated).

Final Prompt Payload Sent to Model

          
Model Review Output (Simulated)

          
View TypeScript Template Function: buildReviewPrompt.ts
buildReviewPrompt.ts
function buildReviewPrompt(code: string, language: string, rules: string[]): string {
  return `You are a ${language} code review expert.

Review rules:
${rules.map((r, i) => `${i + 1}. ${r}`).join("\n")}

Review the code below and output only a JSON array of issues:

\`\`\`${language}
${code}
\`\`\``;
}
Fig 4 · The ReAct Pattern: From Single-Shot Prompts to an Agent Loop Closed-Loop Flow
1. Thought Spot missing info 2. Action (Tools) Search / Query / Test 3. Observation Append result to context Enough Info? Emit final answer Need more context → next reasoning turn (foundation of the Agent Loop)

Three closing habits make prompts maintainable in production: lead with positive instructions (say what you want instead of stacking "don't..." rules), reach for advanced patterns only when needed, and version your prompts with a changelog.

✕ Stacked Negative Constraints

Do not output extra explanations.
Do not add a preamble.
Do not use Markdown formatting.
Do not exceed 20 words.

✓ Positive Instructions First

Summarize the core takeaway in one plain-text sentence using everyday language, under 20 words.

Step-Back Prompting

Ask for high-level principles before tackling a complex question, then feed that answer back as context.

Step-Back Paper ↗

Self-Consistency

Sample the same reasoning prompt multiple times and take the majority vote—great for deterministic answers, at N× token cost.

Self-Consistency Paper ↗

ReAct Pattern

Interleave Reasoning and tool-use Acting inside one loop—the core building block of modern agents.

ReAct Paper ↗
prompts/defect-extractor.md (Prompt Changelog Template)
## Prompt v1.2
- Goal: Extract product defects from customer reviews as a JSON array
- Model: DeepSeek-V4-Pro | Temperature: 0
- Eval Notes: Normal reviews pass; sarcastic reviews occasionally misclassified; empty input returns []
- Changelog:
  - v1.0 → v1.1: Replaced "do not explain" with "output only a JSON array", improving format stability
  - v1.1 → v1.2: Added 2 sarcastic few-shot edge cases

Prompt engineering is the discipline of knowing where text-level control ends—and executing sharply within those bounds. Once role separation, position effects, positive framing, injection isolation, and versioned changelogs become muscle memory, your prompts stop feeling like guesswork. Yet as the Triage Clinic showed, prose alone can never guarantee 100% parseable output. In the next chapter on structured outputs, we move enforcement into code with Zod and JSON Schema.