All study guides

CCA-F · Study guide

Prompt engineering and context management

Mind map

Mind map — prompting & context

🗺 Prompting & Context

  • Structure
    • Role
    • Instructions
    • Examples
    • Output format
    • XML tags
  • Reasoning
    • Step by step
    • Structured output
  • Context window
    • Keep recent
    • Summarise old
    • Prompt caching
  • Principles
    • Specific
    • Positive framing
Summary

Prompting & context — TL;DR

A strong prompt has a clear structure: a role, explicit instructions, relevant context, examples (few‑shot), and a specified output format. Use XML tags to separate sections so Claude parses them reliably. Put long, stable content (system prompt, documents) near the top and mark it for prompt caching to cut cost and latency. Give Claude room to think step by step on hard problems, but ask for structured output when you need machine‑readable results. Manage the context window deliberately: keep the system prompt and recent turns, summarise older history, and never let raw history grow unbounded.

Summary

Prompt caching in practice

Caching pays when the same prefix is sent repeatedly. Rules that matter: the cached block must be a stable, byte‑identical prefix — put the system prompt, reference documents, and tool definitions first, and the variable user input after them. Changing anything inside the cached prefix invalidates it. The win is large on chat apps and high‑volume extraction (cost and latency drop on every repeat call). Design prompts so the static 90% never changes and only the tail varies.

Cheat sheet

Prompting & context — cheat sheet

  • Prompt anatomy: role → instructions → context → examples → output format.
  • XML tags delimit sections (<context>, <example>, <format>) for reliable parsing.
  • Few‑shot examples teach format and edge cases better than description alone.
  • Chain‑of‑thought: ask for step‑by‑step reasoning on complex tasks; parse or hide it.
  • Structured output: specify exact JSON; use tool calls to *force* a schema.
  • Prompt caching: stable content first, cached → big cost/latency win on repeat calls.
  • Context window: keep system + recent turns verbatim · summarise older · drop noise.
  • Be specific and positive: say what to do, not only what to avoid.
Cheat sheet

Structured output that never breaks

  • Strongest: define a tool whose input schema IS your target JSON — tool calls force the structure.
  • Otherwise: show the exact schema + one example output, and say “Return only JSON, no markdown fences, no commentary.”
  • Prefill the assistant turn (e.g. start it with {) to anchor the format.
  • Validate and retry: parse the output; on failure, re‑ask with the error message.
  • Low temperature for extraction and classification.
  • Enums: list the allowed values explicitly; use null for unknown — “never guess” beats silent invention.
  • Long fields: ask for concise values; unbounded prose breaks downstream parsers.
Mnemonic

Mnemonic — “RICE‑F”

“RICE‑F” for prompt structure — Role, Instructions, Context, Examples, Format. When a prompt underperforms, find the missing letter — it's usually E (examples) or F (an explicit output format).

Mnemonic

Mnemonic — “PRUNE”

“PRUNE” the context window — Preserve essentials (system + recent turns), Rank by relevance, Use summaries for older history, Never unbounded, Evict noise. A conversation that only ever grows will eventually degrade — prune deliberately.

Practise this domain with original, exam-style questions.

Start practising free