prompt-and-context-engineering
Guides structuring system prompts, managing context window budget, and organizing what an LLM agent sees at each turn. Use when a user asks to "write a system prompt," "reduce token usage," "fix inconsistent agent behavior," "the model ignores my instructions," "organize context for a long-running agent," or is deciding what belongs in a system prompt vs. a tool description vs. retrieved content vs. conversation history.
selvarajmurugesan90 · Static checks on file
Installation
Read the source and dependencies before running a command. Copying does not install or execute anything.
npx skills add selvarajmurugesan90/ops-engineering-skills --skill prompt-and-context-engineeringAbout this skill
name: prompt-and-context-engineering description: > Guides structuring system prompts, managing context window budget, and organizing what an LLM agent sees at each turn. Use when a user asks to "write a system prompt," "reduce token usage," "fix inconsistent agent behavior," "the model ignores my instructions," "organize context for a long-running agent," or is deciding what belongs in a system prompt vs. a tool description vs. retrieved content vs. conversation history. license: Apache-2.0 compatibility: "Claude Code, GitHub Copilot, OpenAI Codex, Cursor, Gemini CLI" metadata: domain: ai-agent maturity: stable
Prompt and Context Engineering
Purpose
Everything an LLM "knows" during a single call is whatever text is in its context window at that moment — there is no other channel. Prompt and context engineering is the discipline of deciding what goes into that window, in what order, in what format, and how it's kept from growing without bound as an agent runs. Done poorly, this produces agents that ignore instructions, contradict themselves across turns, burn tokens (and money) on irrelevant history, or become unpredictable as conversations grow long. Done well, it is what makes an agent's behavior consistent, debuggable, and affordable to run at scale. This is distinct from model selection or fine-tuning: it's about structuring information for a fixed model, which is usually the highest-leverage, lowest-cost lever available.
When to use
- Writing or revising a system prompt for an agent, especially one with multiple instructions, tools, or output-format requirements.
- The agent's behavior is inconsistent, ignores stated rules, or drifts as a conversation gets longer.
- Deciding what belongs in the system prompt vs. a per-turn user message vs. a tool result vs. retrieved (RAG) content.
- Reducing token usage / latency / cost on a working agent (also see llm-cost-and-latency-optimization).
- Designing how conversation history is truncated, summarized, or windowed for a long-running session.
- Debugging why the model's output format doesn't match what was requested.
Prerequisites & environment
- Know your target model's context window size and, ideally, its documented behavior around very long contexts (many models show degraded attention to middle-of-context content, sometimes called "lost in the middle" — verify current behavior for your specific model rather than assuming a fixed rule).
- Access to token-counting tooling for your model/SDK so budgets are measured, not guessed.
- A test harness or even a handful of representative transcripts you can re-run after each prompt change — prompt engineering without a way to check for regressions is guesswork (see agent-evaluation-and-guardrails).
Step-by-step guidance
-
Separate the four kinds of context and decide what belongs in each:
- System prompt — stable instructions: role, constraints, output format, tool-use policy. Changes rarely, applies to every turn.
- Tool/function descriptions — what each tool does and when to use it (see agent-tool-use-patterns). These are also "context" and count against the budget even though they're not prose.
- Conversation history — prior turns; grows over time and is the most common source of bloat.
- Retrieved/dynamic content — RAG chunks, tool results, file contents; changes every turn and is usually the largest and least trustworthy of the four (see rag-pipeline-design).
-
Structure the system prompt with clear sections, not one paragraph. A common, effective ordering:
# Role You are a release-notes assistant for the Acme platform team. # Task Given a list of merged PR titles, produce a categorized changelog entry. # Constraints - Output must be valid Markdown with exactly these sections: Added, Fixed, Changed. - Do not invent features not present in the PR list. - If a PR title is ambiguous, put it under "Changed" and flag it with (needs review). # Output format ## Added - ... ## Fixed - ... ## Changed - ...Putting constraints and output format in dedicated, labeled sections measurably improves instruction-following compared to burying them in narrative prose — models attend better to structurally salient text.
-
Put the most important instructions near the beginning and end of the prompt, not buried in the middle, and keep the system prompt itself short relative to dynamic content — a 200-line system prompt competing with 50,000 tokens of retrieved content for attention is a design smell, not just a cost problem.
-
Budget the context window explicitly. For a target model context size (e.g. 200K tokens), allocate rough budgets: system + tools (fixed, small), history (bounded via windowing/summarization), retrieved content (bounded per retrieval call), and headroom for the model's own output. Write this budget down; it's what makes "why did this agent run out of context" answerable later.
-
Truncate or summarize history deliberately, not by silently dropping the oldest messages. A common, effective strategy: keep the last N full turns verbatim, and periodically collapse everything older into a running summary maintained by a cheap model call or a deterministic template — never let history grow unbounded and rely on the provider to truncate for you, since default truncation drops arbitrary content, including possibly the system prompt's effect if tool definitions and history compete for the same budget in your SDK.
-
Use few-shot examples sparingly and only for format/style calibration, not to teach facts — examples cost tokens on ever