This commit is contained in:
Lance Martin
2025-07-28 15:15:45 -07:00
parent fadbe7d710
commit d88ca6f649
+23 -27
View File
@@ -1,40 +1,36 @@
# Context
**Context engineering** is the practice of building dynamic systems that provide the right information and tools, in the right format, so that a language model can plausibly accomplish a task.
**Context engineering** is the [art and science of filling the context window with just the right information](https://x.com/karpathy/status/1937902205765607626) so that an AI application can accomplish a task. Context can be characterized along two key dimensions:
Context includes *any* data outside the message list that can shape behavior. This can be:
**By mutability:**
- **Static context**: Immutable data that doesn't change during execution (e.g., user metadata, database connections, tools)
- **Dynamic context**: Mutable data that evolves as the application runs (e.g., conversation history, intermediate results, tool call observations)
- Information passed at runtime, like a `user_id` or API credentials.
- Internal state updated during a multi-step reasoning process.
- Persistent memory or facts from previous interactions.
**By lifetime:**
- **Runtime context**: Data scoped to a single run or invocation
- **Cross-conversation context**: Data that persists across multiple conversations or sessions
LangGraph provides **three** primary ways to manage context:
LangGraph provides three ways to manage context, combining the mutability and lifetime dimensions:
| Type | Description | Mutable? | Lifetime |
|------------------------------------------------------------------------------|-----------------------------------------------|----------|-------------------------|
| [**Runtime Context**](#runtime-context) | data passed at the start of a run | ❌ | per run |
| [**Short-term memory (State)**](#short-term-memory-mutable-context) | dynamic data that can change during execution | ✅ | per run or conversation |
| [**Long-term memory (Store)**](#long-term-memory-cross-conversation-context) | data that can be shared between conversations | ✅ | across conversations |
| Context Type | Description | Mutability | Lifetime | Access Method |
|------------------------------------------------------------------------------|--------------------------------------------------------|------------|-------------------------|-----------------------------------|
| [**Static Runtime Context**](#static-runtime-context) | User metadata, tools, db connections passed at startup | Static | Single run | `context` argument to `invoke`/`stream` |
| [**Dynamic Runtime Context (State)**](#dynamic-runtime-context-state) | Mutable data that evolves during a single run | Dynamic | Single run | LangGraph state object |
| [**Dynamic Cross-Conversation Context (Store)**](#dynamic-cross-conversation-context-store) | Persistent data shared across conversations | Dynamic | Cross-conversation | LangGraph store |
### Runtime Context
### Static Runtime Context
Runtime context is for immutable data like user metadata, tools, db connections, etc. Use this when you have values that don't change mid-run.
Static runtime context represents immutable data like user metadata, tools, and database connections that's passed to an application at the start of a run via the `context` argument to `invoke`/`stream`. This data doesn't change during execution.
!!! version-added "New in LangGraph v0.6: `Runtime.context` replaces `config['configurable']`"
The `Runtime` object is recommended to access static context and runtime-specific information like the store and stream writer.
!!! note
!!! note "Application configuration vs. LLM Context"
Runtime context refers to local context: data and dependencies your code needs to run. It does not refer to:
* The LLM context, which is the data passed into the LLM's prompt.
* The "context window", which is the maximum number of tokens that can be passed to the LLM.
You likely want to use the local context to optimize the LLM's context window. For example, you
could use a user id to fetch a user's name and information from a database to populate the context window with relevant memories.
Specify static context via the `context` argument to `invoke` / `stream`, which is reserved for this purpose:
Runtime context can include data that will be passed to the LLM (e.g., system prompt, tools) as well as application configuration (e.g., model settings, temperature, API keys, database connections) that governs application behavior but is not explicitly passed to the LLM.
Application configuration can be passed via the `context` argument, which replaces `config['configurable']`. And, as before, any context you want to write to state for use in the application can be passed directly as a dictionary to `invoke` / `stream`.
```python
@dataclass
@@ -112,9 +108,9 @@ graph.invoke( # (1)!
See the [tool calling guide](../how-tos/tool-calling.md#configuration) for details.
### Short-term memory (mutable context)
### Dynamic Runtime Context (State)
State acts as [short-term memory](../concepts/memory.md) during a run. It holds dynamic data that can evolve during execution, such as values derived from tools or LLM outputs.
**Dynamic runtime context** represents mutable data that can evolve during a single run and is managed through the LangGraph state object. This includes conversation history, intermediate results, and values derived from tools or LLM outputs. In LangGraph, the state object acts as [short-term memory](../concepts/memory.md) during a run. It holds dynamic data that can evolve during execution, such as values derived from tools or LLM outputs.
=== "In an agent"
@@ -194,8 +190,8 @@ State acts as [short-term memory](../concepts/memory.md) during a run. It holds
Please see the [memory guide](../how-tos/memory/add-memory.md) for more details on how to enable memory. This is a powerful feature that allows you to persist the agent's state across multiple invocations. Otherwise, the state is scoped only to a single run.
### Long-term memory (cross-conversation context)
## Dynamic Cross-Conversation Context (Store)
For context that spans *across* conversations or sessions, LangGraph allows access to **long-term memory** via a `store`. This can be used to read or update persistent facts (e.g., user profiles, preferences, prior interactions).
**Dynamic cross-conversation context** represents persistent, mutable data that spans across multiple conversations or sessions and is managed through the LangGraph store. This includes user profiles, preferences, and historical interactions. For context that spans *across* conversations or sessions, LangGraph allows access to **long-term memory** via a `store`. This can be used to read or update persistent facts (e.g., user profiles, preferences, prior interactions).
For more information, see the [Memory guide](../how-tos/memory/add-memory.md).