cheat-sheets

Prompting and Context Engineering

Patterns for writing prompts and managing what an agent sees. Applies to Claude, OpenAI and local models alike; details differ per model.

Documentation: Anthropic prompt engineering overview · Claude Code best practices

Verification: conceptual guidance with no commands. Effectiveness varies by model, so test on your own tasks.

Writing a Prompt #

PATTERNHOW
Be specificState the goal, audience, format and constraints; vague asks get generic answers
Give context and the whyA reason lets the model generalise (“no jargon because readers are new”)
Show examplesTwo or three input and output pairs fix format better than description
Say what to doPrefer “write plain prose” to “do not use lists”
Separate partsWrap documents, instructions and examples in labelled tags such as <document>
Ask for structureRequest JSON, a table or a fixed template when a program reads the output
Allow “I don’t know”Tell it to say so when evidence is missing, to reduce invented answers
Let it think firstFor hard problems ask for reasoning or enable extended thinking before the answer

Roles and Prompt Layout #

  • Put stable rules in the system prompt and the task in the user message.
  • Put long documents first and the question last.
  • Keep a reusable prompt in a file under version control and change one thing at a time.

Agent Context #

PATTERNHOW
Short project fileKeep CLAUDE.md or AGENTS.md to build and test commands, conventions and gotchas; long files get ignored
Point, do not pasteReference file paths and let the agent read them on demand
Fresh context per taskClear or restart between unrelated tasks; stale history costs tokens and focus
Compact deliberatelySummarise long sessions yourself with a focus hint rather than letting them degrade
SubagentsDelegate searches or reviews so noisy output stays out of the main context
Plan firstAsk for a plan, review it, then implement; cheaper than fixing the wrong build
Verify by runningGive the agent a test or command that proves success
Narrow toolsAllow only the tools a task needs; see MCP safety

Retrieval (RAG) #

  1. Split documents into chunks.
  2. Embed chunks and store the vectors.
  3. Embed the question and fetch the nearest chunks.
  4. Put those chunks in the prompt and ask the model to answer only from them, citing the source.

Tune chunk size and overlap, and test retrieval on its own before blaming the model.

Failure Modes #

SYMPTOMLIKELY CAUSE AND FIX
Invented commands or factsAsk for sources, supply the docs, verify by running
Ignores an instructionToo buried or contradicted; move it up, shorten the prompt, remove conflicts
Output format driftsAdd an example, or use structured outputs or a JSON schema
Repeats or loopsLower the temperature, restate the goal, restart the session
Prompt injection from tool or web contentTreat fetched text as data; limit tool permissions; require confirmation for writes

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.