Contents29 sections
A large language model (LLM) has no continuing conversational state outside a single API call. If an agent is expected to stay coherent, remember preferences, use external information, and complete multi-step tasks, it must assemble the usable information again before every call. The whitepaper calls this dynamic selection, compaction, and injection of information Context Engineering.
Context Engineering deals with the complete request payload: system instructions, tool definitions, examples, conversation history, working state, long-term memories, RAG documents, tool outputs, sub-agent results, and the user's current question. The goal is to give the model what it needs for the current task while controlling irrelevant content, latency, and token cost.
Context Engineering puts per-call information selection, the immediate state of a Session, and the long-term governance of Memory into one engineering loop.
How Context Engineering Turns a Stateless LLM into a Stateful Agent
What a single call needs
Context can be grouped by function. The first group guides reasoning and action through system instructions, tool schemas, and few-shot examples. The second supplies evidence for the model's decision, including long-term memory, external knowledge retrieved through RAG, tool outputs, sub-agent outputs, and associated artifacts such as files and images. The third grounds the current task through the conversation history, temporary state or scratchpad, and the user's current prompt.
| Context component | Main purpose | Lifetime |
|---|---|---|
| System instructions | Define the role, capabilities, and constraints | Across turns or dynamically per request |
| Tool definitions | Describe callable APIs, functions, and parameters | Changes with the agent configuration |
| Few-shot examples | Demonstrate behavior relevant to the task | Selected per task |
| Long-term Memory | Provide user or topic information across sessions | Persisted long term |
| External knowledge and RAG | Provide facts from documents, databases, or APIs | Retrieved dynamically per query |
| Tool and sub-agent outputs | Provide evidence produced during execution | Within the current task |
| Conversation History | Reconstruct the event sequence of the current conversation | Current Session |
| State / Scratchpad | Hold cart contents, plans, calculations, and other work data | Current task or Session |
| User prompt | Define the problem for this turn | Current request |
Every request passes through four stages
Context management forms a loop:
Fetch context: Memory, RAG, recent events, and task metadata
↓
Prepare context: the framework assembles the full request on the hot path
↓
Invoke the LLM and tools: iterate while appending model and tool outputs
↓
Upload context: send durable information to an asynchronous persistence pipelineFetching and preparing happen on the user-facing path and therefore need a latency budget. Memory generation, consolidation, and other expensive processing can run after the response is returned. As a conversation grows, cost, latency, and context rot become more visible: the model may pay less attention to important information. History therefore needs selective pruning, summarization, or other compaction.
Sessions Hold the State of the Current Conversation
Events and state have different jobs
A Session is a self-contained record for one user and one continuous conversation. It contains events appended in time order and mutable working state. Events record user inputs, agent responses, tool calls, and tool outputs. State holds structured temporary data required by the current task, such as cart contents, confirmed filters, or intermediate calculations.
| Session component | Typical contents | Handling |
|---|---|---|
| Events | User messages, agent responses, tool calls, and tool results | Appended in chronological order |
| State | Structured variables, working memory, and scratchpad | Read and mutated by agent logic |
| Persisted record | Full conversation history and metadata | Stored in a reliable Session Store |
A production agent runtime is commonly stateless after a request finishes, so Session history must be persisted. In-memory storage is suitable for development; production needs a reliable database or managed Session service and must preserve deterministic event ordering.
Framework differences should stay behind the application boundary
Agent frameworks do not define Session, Event, and state in the same way. Google ADK exposes an explicit Session containing an Event list and separate state. LangGraph treats mutable state as the Session, with message history and other working data in one state object.
The framework maps its internal events to the request format required by a model API. For example, Gemini requests use a Content list with role and parts. Business logic should depend on a stable internal abstraction while the framework performs the conversion, preventing a provider-specific message schema from spreading through the application.
Multi-agent systems should share semantic state
In a multi-agent system, each agent may keep a private history and return a task result to a parent agent, or the agents may share richer Session state. A2A messages are useful for structured tasks and results, but they do not by themselves solve history-schema translation across frameworks.
Cross-framework collaboration benefits from a framework-agnostic Memory layer. It stores processed summaries, entities, and facts in common structures such as strings or dictionaries. Session keeps raw events; Memory provides a shared knowledge layer that heterogeneous agents can read.
Why Long Conversations Need Compaction
Long context raises four costs at once
Session history may be read from a central store and sent to the model on every turn. Without management, the context limit, API bill, response speed, and answer quality all come under pressure.
| Cost | Observable effect | Direction of treatment |
|---|---|---|
| Context window | The request fails after history exceeds the model limit | Control tokens or summarize |
| API cost | More input and output tokens increase the cost per turn | Remove irrelevant old content |
| Latency | More text takes longer to transfer and process | Shorten the hot-path payload |
| Quality | Noise makes important information harder to attend to | Keep high-value facts and recent context |
The aim of compaction is to shrink history to a model-usable range while retaining facts, decisions, and constraints needed by the current task. The stored history and the view sent to the model can be separated: compaction may change only the view for one call, or persist a reusable summary for later turns.
Three Session compaction strategies
- Keep the last N turns: Use a sliding window and send only the most recent turns. It is simple and low-latency, and fits conversations where old material has little value.
- Token-based truncation: Count from the newest message backward until a token budget is reached. This directly controls payload size, but it can abruptly discard an important earlier fact.
- Recursive summarization: Replace older messages with an LLM-generated summary and combine it with recent verbatim messages. This preserves more of the task narrative, but adds a model call and requires summary-quality checks.
Expensive work such as recursive summarization should run in the background, and its result should be persisted so that each turn does not repeat the computation. The system should also record which source events a summary covers, preventing already-compacted text from being sent to the model again without reason. Compaction can be triggered by turn count, token count, time, or an event such as task completion.
How Memory Extracts Durable Information from Conversations
The boundary between Memory and Session
A Session is the immediate work surface of one conversation. Memory stores information extracted from a conversation or another source for future interactions. The two systems reinforce each other: Sessions provide the main raw material for Memory generation, while Memory helps compact Sessions and reduces the amount of raw history passed between turns.
A Memory generally contains content and metadata. Content should use framework-agnostic strings, dictionaries, or JSON where possible. Metadata can record a memory ID, owner, source, time, labels, and governance information. Memories describe information that happened or was explicitly expressed; predictive guesses should not be treated as user facts.
RAG and Memory have different responsibilities
RAG and Memory both supply information to context, but their sources, isolation levels, and write patterns differ. RAG fits shared, stable external facts. Memory fits personalized information scoped to a user and updated through interaction.
| Dimension | RAG Engine | Memory Manager |
|---|---|---|
| Primary goal | Inject external facts into context | Build a continuous, personalized user state |
| Data source | Pre-indexed PDFs, wikis, documents, and APIs | Dialogues and explicit inputs from users and agents |
| Isolation | Usually shared and read-only | Usually isolated by user or tenant |
| Information | Static, factual, and relatively authoritative | Dynamic, user-specific, and source-uncertain |
| Write pattern | Commonly offline batch or administrative ingestion | Event-based, session-end, or Memory-as-a-Tool |
| Read pattern | Retrieved as a tool when the query needs external data | Preloaded automatically or queried on demand |
| Useful analogy | A research library that makes the agent an expert on facts | A personal assistant that makes the agent an expert on the user |
A Memory Manager is more than a vector-similarity database. It must also extract, deduplicate, resolve conflicts, update, and invalidate information. RAG gives an agent domain facts, while Memory gives it continuity with a user. Both can participate in the same Context Engineering loop.
Memory types and organization patterns
By information content, Memory can be declarative or procedural. Declarative Memory answers “what” and contains facts, history, and user profiles. Procedural Memory answers “how” and contains reusable workflows, tool-call sequences, and problem-solving strategies.
Three common organization patterns are useful. Collections store multiple independent events or observations as a searchable set. A Structured User Profile keeps stable facts such as names, preferences, and account details in a continuously updated profile. A Rolling Summary consolidates the user-agent relationship into one evolving natural-language document and is often used to control the token count of a long Session.
For storage, vector databases retrieve natural-language atomic facts by semantic similarity. Knowledge graphs represent entities and relationships and support structured relational queries. A hybrid design combines graph structure with vector representations to support both relationship reasoning and semantic search.
Memory can also be classified by its creation path. Explicit memories come from a direct “remember this” request. Implicit memories are extracted from conversation, so their trigger and confidence need separate governance. Internal Memory is managed inside the agent framework and is convenient for starting out. External Memory uses a dedicated service for generation, storage, and retrieval and can scale independently. Multimodal inputs can be converted into textual insights, or the media itself can be retained as memory content; the latter raises additional storage, retrieval, and model-interface requirements.
Memory Generation Requires Extraction, Consolidation, and Provenance
The four-stage memory-generation pipeline
Memory generation resembles an LLM-driven ETL pipeline, but the model also helps judge the content:
- Ingestion: Accept conversation history, direct memories, or another raw source.
- Extraction & Filtering: Extract meaningful information according to predefined topics. If no information matches, create no Memory.
- Consolidation: Compare new information with existing memories and decide how to handle duplicates, conflicts, and change through update, creation, or deletion.
- Storage: Persist the final memory in a vector database, knowledge graph, or another durable store for future retrieval.
Consolidation handles updates, conflicts, and forgetting
Without consolidation, Memory becomes a noisy log full of duplicate and contradictory entries. A consolidation workflow commonly retrieves old memories similar to the new information and then asks an LLM to choose an operation:
- UPDATE: Change an existing memory with new or corrected information.
- CREATE: Create an independent memory when the insight is novel and unrelated.
- DELETE / INVALIDATE: Remove or invalidate an old memory when it is wrong, irrelevant, or no longer useful.
- FORGET: Prune stale or low-confidence content using freshness, confidence, or TTL.
User preferences and state change over time. The system needs to let newer information supersede older information and retain enough lineage to explain that change. Deletion must also account for derivation: when one Memory depends on several sources and a user revokes one source, the system needs to find and process the affected derived memories.
Provenance sets the trust level of a Memory
A Memory's source and evolution history influence consolidation priority and the weight it should receive during inference. Common sources deserve separate assessments of trust and stability:
| Source | Typical characteristic | Governance direction |
|---|---|---|
| Bootstrapped data | Preloaded from internal systems such as a CRM; useful for cold start | Record the system source and update time |
| Explicit user input | A form or direct instruction with clear intent | Preserve user confirmation and revocation |
| Implicit conversational extraction | Inferred from natural dialogue and potentially ambiguous | Lower default trust and retain the evidence chain |
| Tool output | Returned by an external call and prone to staleness or local context | Prefer short-lived caching and be cautious about durable storage |
When sources conflict, the system can prefer the more trusted source, the newer source, or corroboration across multiple data points. Memory Poisoning is another provenance problem: malicious input can inject false facts or instructions into long-term storage. Validation, sanitization, and access control are needed before committing content to durable Memory.
When to Retrieve Memory and How to Place It in Context
Rank relevance, recency, and importance together
Memory Retrieval must find useful information within a latency budget. Ranking only by vector similarity can surface an old or trivial memory that is semantically close. A stronger ranking model combines at least three dimensions:
| Score dimension | Question | Common treatment |
|---|---|---|
| Relevance | How related is the memory to the current conversation? | Semantic search and query rewriting |
| Recency | How fresh is the memory? | Time decay or freshness weight |
| Importance | How significant is it for the user or task? | Mark at generation time and blend at retrieval |
Query rewriting, reranking, and specialized retrievers can improve accuracy, but extra LLM calls and ranking work add latency. When results are stable and do not become stale quickly, caching can offset part of the cost. A high-quality Memory Corpus usually matters more than sophisticated retrieval tricks.
Proactive retrieval and tool-based retrieval
Proactive retrieval loads Memory at the start of every turn. It fits stable information such as a user profile, but it makes turns that do not need Memory pay the retrieval cost. Reactive retrieval, also called Memory-as-a-Tool, gives the Agent a query tool and lets it decide when Memory is needed. This reduces unnecessary retrieval, but adds a tool-decision call and may fail when the Agent does not know that a relevant memory exists.
The tool description can state what kinds of information are available, helping the Agent decide more accurately. Either strategy needs test sets for missing memories, irrelevant memories, and latency.
Boundaries for system-instruction and dialogue injection
Stable global information that should be present on every turn can be appended to system instructions. This keeps dialogue history clean and gives the Memory high instruction authority; it also creates a risk that the Agent will force every topic to relate to the profile. The framework must support dynamic system-prompt construction and handle multimodal memories that do not fit naturally into text-only instructions.
Short-lived or task-specific memories can be injected into conversation history or returned as tool output. This is flexible, but it can add token noise and cause the model to mistake a Memory for something actually said in the original dialogue. The injection should identify source, perspective, and trust level. A hybrid strategy often balances stable profiles with temporary context.
Procedural Memory Records How to Execute
Declarative and procedural Memory solve different problems
Most commercial Memory systems are optimized for declarative information: user preferences, history, and “what” facts. Procedural Memory stores “how to do it” by extracting reusable playbooks from successful interactions.
Procedural Memory also has extraction, consolidation, and retrieval stages, but its consolidation target is a workflow. It absorbs successful methods, patches existing steps, and removes outdated or ineffective procedures. Retrieval returns an executable plan for a complex task rather than data for a factual answer. Procedural Memory may therefore need a different schema and evaluation standard from declarative Memory.
The boundary between procedural Memory and fine-tuning
Fine-tuning is an offline training process that changes model weights, often with a long cycle and a broad effect. Procedural Memory enables fast online adaptation by injecting an appropriate playbook during inference without changing the model weights. It fits updateable procedures and strategies, while still requiring versioning, provenance, rollback, and security review.
Production Safety, Reliability, and Evaluation
Production constraints for Sessions and Memory
Production Session governance must cover privacy, security, data integrity, lifecycle, and performance. Every access should authenticate and authorize the user or tenant, with ACLs enforcing strict isolation. PII should be redacted before Session data is written. Sessions need TTL, archive, and deletion policies, and events must be appended in deterministic order.
Session sits on the hot path of every response, so read and write operations, network transfer, and context compaction need explicit budgets. Memory generation usually requires LLM calls and database writes, so a dedicated service should process it asynchronously. The Agent pushes raw data, the service extracts and consolidates it in the background, the result is persisted, and the Agent retrieves it during a later interaction.
At high concurrency, the system must handle races when the same Memory is updated at the same time. Transactions or optimistic locking can help, while queues absorb spikes. Transient errors need exponential-backoff retries; persistent failures should go to a dead-letter queue. Global deployments need storage-level multi-region replication while preserving one transactionally consistent logical view.
Three evaluation layers
Memory evaluation should answer three separate questions: Is the system remembering the right content? Can it retrieve that content when needed? Does using it help the Agent complete the task?
| Evaluation layer | Metrics | What it measures |
|---|---|---|
| Generation quality | Precision, Recall, F1-Score | Whether the system stores accurate, relevant information and captures the facts it should keep |
| Retrieval performance | Recall@K, Latency | Whether the correct Memory appears in the top K results and retrieval fits the hot-path budget |
| End-to-end outcome | Task success, LLM Judge, and human baselines | Whether Memory improves the Agent on real tasks |
Start with a manually maintained golden set. Then iterate through a baseline, failure analysis, prompt or retrieval tuning, and re-evaluation. Generation and consolidation may run in the background, but throughput, failure rate, and high-concurrency stability also need measurement.
A minimal implementation checklist
- Define the boundary between Session, Event, State, and Memory, including which information is valid only for the current task.
- Record user or tenant scope, source, time, freshness, confidence, and deletion paths for every Memory.
- Start with a sliding window or token truncation for Session control, then add recursive summarization when quality requirements justify it.
- Put Memory generation, consolidation, and cleanup in a non-blocking background pipeline, and record lineage between source events and derived memories.
- Choose system instructions, dialogue injection, or Memory-as-a-Tool for each Memory type, and label the source explicitly.
- Run continuous regression over quality, retrieval, task success, latency, throughput, and security cases to measure the practical value of each architecture change.
Context Engineering connects request assembly, the immediate state of a Session, and the long-term governance of Memory in one engineering loop. Session provides low-latency continuity for the current conversation; Memory provides persistence and personalization across conversations. Reliability comes from information selection, traceable data lifecycles, and continuous evaluation.
REFERENCES
References
- 01Context Engineering: Sessions, Memory — Kimberly Milam and Antonio Gulli
- 02Context Engineering: Sessions, Memory — Google Drive PDF
- 03New Whitepaper: Context Engineering for Intelligent Agents — Kimberly Milam
- 04Agent Engine Sessions overview — Google Cloud
- 05Generate memories with Agent Engine Memory Bank — Google Cloud
- 06Multi-agent systems — Google Agent Development Kit
- 07Memory — LangGraph documentation
- 08Model Armor overview — Google Cloud