Context engineering – the practice of designing how an agent's context window is structured and dynamically modified – is becoming increasingly important as agents become long-running and stateful, rather than just simple workflows.
上下文工程 - 设计代理的上下文窗口如何结构化并进行动态修改的实践变得越来越重要,因为代理变得长期运行和状态,而不仅仅是简单的工作流程。
Context is a valuable resource, and requires careful context engineering to design how the context window is managed over time. But to engineer context, we first need to break down the actual components (or “anatomy”) of a context window, and how these components are managed over time.
上下文是一种宝贵的资源,需要仔细的上下文工程来设计上下文窗口如何随着时间的推移管理。但是对于工程师的环境,我们首先需要分解上下文窗口的实际组件(或“解剖学”),以及如何随着时间的推移来管理这些组件。
Breaking down an agent’s context window
分解代理的上下文窗口
An agent’s context window consists of the following components:
代理的上下文窗口由以下组件组成:
System prompt : Defines the agent's architecture and control flow, providing high-level behavioral instructions and explaining core concepts like memory, blocks, and files.
系统提示:定义代理的体系结构和控制流,提供高级行为指令,并解释诸如内存,块和文件之类的核心概念。
: Defines the agent's architecture and control flow, providing high-level behavioral instructions and explaining core concepts like memory, blocks, and files. Tool schemas : Specifications that define available tools and their interfaces, enabling the agent to understand what actions it can perform.
:定义代理的体系结构和控制流,提供高级行为指令,并解释诸如内存,块和文件之类的核心概念。工具模式:定义可用工具及其接口的规格,使代理商能够了解其可以执行的操作。
: Specifications that define available tools and their interfaces, enabling the agent to understand what actions it can perform. System metadata: Stores statistics and metadata about the agent’s state (e.g. the size of the full message history) .
:定义可用工具及其接口的规格,使代理能够了解其可以执行的操作。系统元数据:存储有关代理状态的统计和元数据(例如,全部消息历史记录的大小)。
Stores statistics and metadata about the agent’s state (e.g. the size of the full message history) . Memory blocks : Persistent units of context (e.g. for long term memory or working context), managed by the agent itself or other background agents.
存储有关代理商状态的统计数据和元数据(例如,完整消息历史记录的大小)。内存块:由代理本身或其他背景代理管理的上下文持续单位(例如,对于长期内存或工作上下文)。
: Persistent units of context (e.g. for long term memory or working context), managed by the agent itself or other background agents. Files & Artifacts : Files (PDFs, source code, etc.) that the agent can access and manipulate.
:由代理本身或其他背景代理管理的上下文持续单位(例如,对于长期内存或工作上下文)。文件和工件:代理可以访问和操纵的文件(PDF,源代码等)。
: Files (PDFs, source code, etc.) that the agent can access and manipulate. Message buffer: The message stream containing messages (user, assistant, and system, and tool calls and tool returns).
:文件(PDF,源代码等),代理可以访问和操纵。消息缓冲区:包含消息的消息流(用户,助理和系统,以及工具调用和工具返回)。
Context management refers to both how this context window is designed (through configuration of files, blocks, tools, and prompts), as well as how this context window evolves over time. The context window can be controlled directly by the underlying system (the “LLM OS” or “AI OS”) or with agentic tool calling (which is executed by the OS).
上下文管理既指该上下文窗口的设计(通过配置文件,块,工具和提示),以及此上下文窗口如何随着时间的推移而演变。上下文窗口可以通过基础系统(LLM OS”或“ AI OS”)或代理工具调用(由OS执行)直接控制。
For example, external context can be pulled into the context window via tools, such as with MCP (Model Context Protocol) or built-in tools for conversational or file search:
例如,可以通过工具将外部上下文插入上下文窗口中,例如使用MCP(模型上下文协议)或内置工具进行对话或文件搜索:
Tool calls can also be used to modify specific parts of the context window, such as memory blocks or file blocks:
工具调用也可以用于修改上下文窗口的特定部分,例如内存块或文件块:
Designing these mechanisms for context arrangement and evolution over time is what we refer to as “context engineering”, and is what defines the long-term behavior of your agents.
我们将这些机制随着时间的推移设计为“上下文工程”,这是定义代理人的长期行为的方法。
The LLM Operating System: Automated Context Engineering
LLM操作系统:自动上下文工程
To understand context engineering, it's helpful to think of frameworks like Letta as an LLM OS — an operating system for language models. Just as traditional operating systems manage hardware resources (CPUs, GPUs, I/O devices) and provide abstractions for applications to use the underlying hardware, an LLM OS manages context windows and provides abstractions for context engineering the underlying LLM.
要了解上下文工程,将Letta等框架视为LLM OS(一种用于语言模型的操作系统)是有帮助的。就像传统操作系统管理硬件资源(CPU,GPU,I/O设备)并为应用程序使用基础硬件提供抽象一样,LLM OS也管理上下文Windows Windows,并为上下文工程设计了基础LLM的摘要。
In a traditional operating system, resources are scoped into multiple layers:
在传统的操作系统中,资源被范围划分为多层:
Typical operating system architecture (from linux-kernel-labs.github.io)
典型的操作系统体系结构(来自linux-kernel-labs.github.io)
Like resources in traditional operating systems, in an LLM OS, context is scoped to the application layer (i.e. user space) and the system layer (i.e. kernel space):
像传统操作系统中的资源一样,在LLM OS中,上下文范围范围范围范围(即用户空间)和系统层(即内核空间):
Kernel Context - Managed context that can be modified through APIs and tool calling (like kernel memory). This represents the underlying agent configuration (the system prompt and tools), as well as managed context such as memory blocks and files.
内核上下文 - 可以通过API和工具调用(例如内核内存)修改的托管上下文。这代表了基础代理配置(系统提示和工具),以及托管上下文,例如内存块和文件。
- Managed context that can be modified through APIs and tool calling (like kernel memory). This represents the underlying agent configuration (the system prompt and tools), as well as managed context such as memory blocks and files. User Context - The message buffer containing conversations and external context (like user processes), as well as system calls (tools) to modify the kernel context.
- 可以通过API和工具调用(例如内核内存)修改的托管上下文。这代表了基础代理配置(系统提示和工具),以及托管上下文,例如内存块和文件。用户上下文 - 包含对话和外部上下文(例如用户进程)的消息缓冲区以及系统调用(工具)以修改内核上下文。
Kernel Context: System-Managed Context
内核上下文:系统管理的上下文
Kernel context represents the managed, mutable state of an agent—analogous to kernel memory in an operating system. This layer enables agents to maintain memory and work with persistent data structures that evolve over time. Crucially, the kernel context is rendered into the context window where the LLM can observe it, but modifications happen through controlled interfaces.
内核上下文代表代理的管理,可变状态 - 操作系统中内核内存的含量。该层使代理能够保持内存并与随着时间的流逝而发展的持久数据结构一起工作。至关重要的是,内核上下文被渲染到LLM可以观察它的上下文窗口中,但是通过受控界面进行修改。
The System Call Interface
系统调用接口
Just as operating systems provide system calls for user processes to interact with kernel space, the LLM OS provides built-in tools that act as the system call interface:
正如操作系统提供了与内核空间交互的用户流程的系统调用一样,LLM OS提供了充当系统调用接口的内置工具:
Memory operations : Tools like memory_replace, memory_rethink, and memory_append allow controlled modification of memory blocks
内存操作:诸如MONEME_REPLECE,MONEME_RETHINK和MONEME_APPEND之类的工具允许对内存块的控制修改
: Tools like memory_replace, memory_rethink, and memory_append allow controlled modification of memory blocks File operations : Tools like open, close, and grep manage file state in kernel space
:诸如MONEME_REPLECE,MONEME_RETHINK和MONEME_APPEND之类的工具允许对内存块的受控修改文件操作:诸如OPEN,CLOSS和GREP MANGATE FILE STACE中的工具
: Tools like open, close, and grep manage file state in kernel space Custom operations: Custom tools can call APIs to modify underlying state
:诸如Open,Close和GREP之类的工具在内核空间自定义操作中管理文件状态:自定义工具可以调用API来修改基本状态
Combining these tools allows for management of agent context windows via the LLM. These tools can be used both by the agent itself to manage its own memory and context, or by other specialized agents (e.g. sleep-time agents which process information in the background).
结合这些工具可以通过LLM管理代理上下文Windows。代理本身可以使用这些工具来管理自己的内存和上下文,或其他专门的代理(例如,在后台处理信息的睡眠时间代理)。
Memory Blocks
内存块
Introduced by MemGPT, memory blocks are reserved portions of the context window designed for persistent memory. Each memory block has several key properties:
由Memgpt引入的,内存块是为持久内存而设计的上下文窗口的保留部分。每个内存块都有几个关键属性:
Size limits : Hard constraints preventing overflow
尺寸限制:阻止溢出的硬约束
: Hard constraints preventing overflow Labels and descriptions : Metadata that guides what information should be stored
:坚硬的约束阻止溢出标签和描述:指导应该存储哪些信息的元数据
: Metadata that guides what information should be stored Access patterns: Including read-only flags for protected memory
:元数据指导应该存储哪些信息的访问模式:包括用于保护内存的只读标志
Memory blocks enable agents to maintain state across conversations, learn from interactions, and build up knowledge over time, and can also be used as “working memory” to achieve complex tasks like deep research. Memory blocks can also be shared across multiple agents.
内存块使代理能够在对话中保持状态,从互动中学习并随着时间的推移建立知识,并且也可以用作“工作记忆”,以实现诸如深入研究之类的复杂任务。内存块也可以在多个代理之间共享。
Files
文件
Files provide a familiar abstraction for data management within the context window. The file metaphor is particularly powerful because many LLMs are post-trained on coding tasks, making them naturally adept at file operations. Files can be"open" (loaded into context) or"closed" (stored with metadata only), and also searched with common operations like vector search or grep, which translates well to agent capabilities
文件为上下文窗口中的数据管理提供了熟悉的抽象。文件隐喻特别强大,因为许多LLM在编码任务上进行了培训,因此自然而然地擅长文件操作。文件可以“打开”(加载到上下文中)或“封闭”(仅使用元数据存储),并且还使用诸如vector搜索或GREP之类的常见操作进行搜索,该操作很好地转化为代理功能
Artifacts represent a special category of editable files (often with extra dependencies or file groupings). These allow agents to iteratively modify content—whether code, documentation, or creative writing—through successive refinements.
工件代表可编辑文件的特殊类别(通常具有额外的依赖项或文件分组)。这些允许代理通过连续改进进行迭代修改内容(无论是代码,文档还是创意写作)。
User Context: The Message Buffer
用户上下文:消息缓冲区
The user context, or message buffer, represents the"user space" of our LLM OS. Like processes in a traditional OS, this is where the actual work happens—conversations unfold, tools are called, and the agent interacts with the outside world. Tools in the user-space can be used to pull in external context (e.g. via MCP) or modify context in the kernel space.
用户上下文或消息缓冲区表示我们LLM OS的“用户空间”。就像传统操作系统中的过程一样,这是实际工作发生的地方 - 转换,工具被调用,并且代理与外界互动。用户空间中的工具可用于拉出外部上下文(例如通过MCP)或在内核空间中修改上下文。
The buffer contains several message types:
缓冲区包含几种消息类型:
User messages : Direct input from users
用户消息:用户直接输入
: Direct input from users Assistant messages : The agent's responses
:来自用户助手消息的直接输入:代理的回复
: The agent's responses System messages : Framework or system-generated notifications (like system logs)
:代理的响应系统消息:框架或系统生成的通知(例如系统日志)
: Framework or system-generated notifications (like system logs) Tool calls and responses: The log of both system calls and other tools, as well as their results
:框架或系统生成的通知(如系统日志)工具调用和响应:系统调用和其他工具的日志及其结果
Tool Categories: System Calls vs User Programs
工具类别:系统调用与用户程序
In our OS analogy, tools fall into two distinct categories:
在我们的OS类比中,工具分为两个不同的类别:
System Tools (Built-in system calls) are provided by the LLM OS itself and have privileged access to modify agent state internals reflected in the compiled kernel context (blocks & files).
系统工具(内置系统调用)由LLM OS本身提供,并且具有在编译内核上下文(块和文件)中反映的“修改代理状态内部”的特权访问权限。
Custom Tools (User programs) are defined by developers and run in user space with limited privileges. They serve as the primary mechanism for dynamically pulling in external context, such as through:
自定义工具(用户程序)由开发人员定义,并在特权有限的用户空间中运行。它们是在外部环境中动态拉动的主要机制,例如通过:
Search tools for real-time information or accessible external data
搜索实时信息或可访问的外部数据的工具
Model Context Protocol (MCP) for standardized context retrieval
标准化上下文检索的模型上下文协议(MCP)
These custom tools act as the agent's gateway to the outside world, pulling relevant external information into the context window while maintaining the integrity of the kernel state through proper system call interfaces.
这些自定义工具充当代理到外界的网关,将相关的外部信息拉到上下文窗口中,同时通过适当的系统调用接口保持内核状态的完整性。
Context Engineering & Agent Memory
上下文工程和代理记忆
Proper context management and engineering allows you to build agents that actually have long term memory and the ability to use their memory to solve more complex tasks and learn over time. Context and memory management make it possible to build agents that improve, rather than derailing and degrading.
正确的上下文管理和工程使您可以构建实际上具有长期内存的代理,并能够使用其内存来解决更复杂的任务并随着时间的流逝而学习。上下文和内存管理使建立改进的代理,而不是脱离和退化。
Conclusion
结论
You can get started with building agents with proper context engineering with Letta. The Agent Development Environment (ADE) makes it easy to visualize your context window so you can engineer your agent’s context to access long-term memory, large data sources and files, and external tools via MCP.
您可以开始使用Letta的适当上下文工程的建筑代理。代理开发环境(ADE)使您可以轻松地可视化上下文窗口,以便您可以通过MCP来设计代理的上下文,以访问长期内存,大数据源和文件以及外部工具。
Get started today with Letta Cloud!
今天开始使用Letta Cloud!