03. Core Components
Prompts, System Prompts and Context Management
Prompts steer the model; system prompts lock role and policy; context management decides what fits in the window each turn. Together they are how Agentic AI systems control what the LLM sees.
What you will learn
- Distinguish system, developer, user, and context messages
- Write a clear system prompt for an agent role
- Design a context pack with token budgets
- Separate trusted policy from untrusted data
- Answer interview questions with short, solid answers
Definition
A prompt is the full input the model receives for a turn. A system prompt is the stable, high-priority instruction block that defines role, goals, style, and hard constraints. Context management is the engineering practice of selecting, ranking, redacting, and packing memory, docs, and tool results into that prompt under a token budget.
In agents, prompting is not a one-shot essay β it is a repeated contract: every loop rebuilds context, calls the model, executes tools, then packs observations again.
Introduction
This lesson (#2 in Core Components) follows Large Language Models. The LLM is the engine; prompts and context are the fuel and air intake.
Next lessons cover planning, memory types, and tool calling β each feeds this context pack.
Why prompts and context matter
- They dominate agent quality more than model swaps alone
- Bad context causes tool misuse and hallucinations
- Clear system policy is your first safety layer
- Token budgets force deliberate retrieval and summarization
Prompt layers
Name the layers so teams do not paste everything into one blob.
- System - identity, non-negotiable rules, output/tool norms
- Developer / task - workflow steps, schemas, examples for this product
- User - the request (treat as untrusted data)
- Dynamic context - memory, RAG chunks, tool results, state summary
- Assistant history - prior turns (trim or summarize aggressively)
System prompts for agents
A good agent system prompt is short, testable, and versioned. It states who the agent is, what success looks like, which tools exist, and what it must never do.
- Role + scope - one paragraph max
- Goals and done criteria - measurable when possible
- Tool use policy - when to call, when to ask, when to refuse
- Safety - PII, spend, irreversible actions
- Format - JSON / function-call discipline if required
- Anti-pattern - novel-length prompts nobody evals
Context management
Context management is a pipeline, not a paste. Agents fail when teams dump transcripts, whole repos, or raw HTML into the window.
- Collect candidates - messages, memories, retrieved docs, tool outputs
- Filter - secrets, irrelevant noise, prompt-injection strings
- Rank - relevance to the current subgoal
- Budget - hard caps per section (system / history / RAG / tools)
- Assemble - labeled sections the model can cite
- Log - store the pack ID for debugging and evals
Practical patterns
- Pinned system prompt + sliding window of recent turns
- Summarize older history instead of infinite chat replay
- RAG with citations; prefer short chunks over chapters
- Tool results as structured observations, not giant blobs
- Scratchpad / state object for goals, plan, and progress
- Delimiter tags (e.g. CONTEXT, USER) to reduce instruction bleed
Prompt injection and trust
Anything retrieved from users, the web, or tools can try to override your system rules. Defense is architectural, not only wording.
- Treat external text as data inside labeled blocks
- Enforce tool allowlists in code, not only in prose
- Prefer structured tool calls over free-form βrun this shellβ text
- Re-assert critical policy after untrusted content when needed
Program / implementation notes
Build a ContextBuilder module with unit tests. Version system prompts like code. Eval prompt changes on agent tasks before shipping.
- Keep prompts in files/templates, not buried in app code strings only
- Measure tokens per section; alert on overflow
- Golden tests: same goal + fixtures β expected tool choice
- A/B system prompt changes with offline eval sets
- Next: Reasoning, Planning and Task Decomposition
Advantages and limitations
- Advantage: fast control of agent behavior without retraining
- Advantage: context packs make long tasks feasible
- Advantage: clear trust boundaries improve safety
- Limitation: prompts are brittle without evals
- Limitation: windows still overflow on large workspaces
- Limitation: injection risk rises with retrieved content
Key points
- System prompts define trusted role and policy.
- User and retrieved text are untrusted data.
- Context management = collect, filter, rank, budget, assemble, log.
- Agents rebuild context every loop β design for that.
- Short, versioned, evaluated prompts beat long untested ones.
- Enforce dangerous limits in code, not only in prompt prose.
FAQs
System prompt vs user prompt?
The system prompt sets durable role and rules. The user prompt states the current request. Context management adds supporting evidence under a budget.
How long should a system prompt be?
As short as it can be while still testable. If a rule is not enforced or evaluated, cut it or move it into code.
What if the context window is too small?
Summarize history, retrieve only top-k docs, compress tool outputs, and keep a structured state object instead of replaying everything.
Can prompt engineering replace tools and memory?
No. Prompts steer the model; tools and memory supply facts and side effects prompts cannot invent reliably.
Interview questions
- What is a system prompt?
Answer A stable, high-priority instruction block that defines the modelβs role, constraints, and operating policy for the session or agent.
- What is context management in Agentic AI?
Answer Selecting and packing the right memory, documents, and tool results into each model call under token and safety constraints.
- Why separate system instructions from user content?
Answer So untrusted user or retrieved text cannot easily override policy; trusted rules stay in a distinct channel enforced by the app.
- How do you prevent context overflow in long agent runs?
Answer Use section budgets, summarization, retrieval top-k, structured state, and drop stale tool payloads after they are consumed.
- What belongs in an agent system prompt?
Answer Role, goals/done criteria, tool-use policy, safety limits, and required output/tool format β kept short and versioned.
- What is prompt injection?
Answer An attack where untrusted content tries to override system instructions; mitigate with isolation, allowlists, and code-level controls.
- How do you evaluate prompt changes?
Answer Run golden agent tasks offline, compare tool choices and success rates, and ship only if quality/cost metrics hold.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Write a half-page system prompt for a coding agent (role, tools, safety, done).
- List five context sections with a token budget for each.
- Mark each of: system, user email, RAG chunk, tool JSON as trusted or untrusted.
- Sketch collect -> filter -> budget -> assemble for one support-turn.
- Answer interview questions 1, 2, and 6 in two sentences each.