Text of code.visualstudio.com › docs › agents › concepts › context as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Understand context in AI agents

AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. Understand context in AI agents View as Markdown Ask in VS Code Ask in VS Code Insiders Context is the information available to an AI agent and its language model while they work on your request.

It can include conversation history, files from your workspace, tool outputs, custom instructions, and references you add to your prompt. The language model reasons over this information to decide what to say or do next. The agent can use tools to gather more context, take actions, and evaluate the results. This article explains how VS Code assembles and discovers context, and how to work within context window limits.

Why context matters A prompt with relevant files, clear instructions, and focused history produces better results than a vague prompt with no context. The language model can only reason over information included in its current context. You don't need to identify every relevant file before you start. During the agent loop , the agent can search your workspace, read files, run commands, and add the results to the context for its next step.

Explicit references are useful when you already know which information the agent should consider. Conversation history is scoped to a chat and isn't automatically shared with other chats or sessions. Context can be passed explicitly, such as in a delegated task or a handoff . Persistent sources, such as custom instructions or agent memory , can also provide information across conversations. Context, knowledge, and memory Several sources help an agent respond, but they have different lifetimes: Source What it provides What persists Model training General capabilities and knowledge learned before your request.

The model's trained knowledge is separate from your chat. It doesn't automatically include your project. Current context Information included in a particular model request, such as your message, files, and tool results. Later requests need to include that information again, or retrieve it when needed. Conversation history The discussion and tool activity in the current chat. Saved history can outlast a request, but the model only sees the portion included in its context, which can be summarized.

Other chats don't automatically inherit it. Custom instructions Reusable guidance, such as project conventions and test commands. Instructions remain in their configured source and apply when the harness discovers and includes them. Memory Selected preferences or project facts stored separately from the conversation. Retention and sharing depend on the memory system. A saved fact must be retrieved to inform a response.

Adding context is not the same as training a model. Correcting an answer can guide later responses while that correction remains in context, but doesn't by itself update the trained model or establish memory in other chats. Separate provider policies govern data retention and use for training. See where information goes . For example, put a required project test command in custom instructions rather than relying on an earlier chat.

Use memory for supported preferences and reusable facts, and review saved information for accuracy. How VS Code assembles context Each time the agent sends a request to the language model, VS Code assembles a prompt from multiple sources: System instructions : built-in guidelines that define the agent's behavior. Customizations : AI customizations you set up, including custom agents, skills, and custom instructions. User message : the current request or follow-up message you send to the agent.

This is one part of the full prompt sent to the model. Conversation history : the messages exchanged so far in the current chat. Implicit context : editor information, such as the active file or selection, when the interface, harness, and context settings include it. Explicit references : files, editor context, web content, and other sources you reference with # -mentions. Tool outputs : results from file reads, terminal commands, codebase search results, and other tool calls during agent sessions.

The context changes as the agent works. For example, the output of a code search can lead the agent to read a file, and that file can then inform the next model request. Information that isn't included in a request is unavailable to the model, but the agent can use tools to discover and add more information. The agent can also retrieve context from external services through Copilot connectors Copilot connectors is in preview and might change before it becomes generally available. or other MCP tools.

Connecting a service doesn't automatically load its data into the context. Results become tool output when an agent retrieves them. Workspace indexing VS Code uses a semantic index to find code by meaning rather than only by exact keywords. The index helps the agent locate relevant snippets quickly, especially in a large codebase. It doesn't add your entire workspace to every model request. VS Code builds and maintains the index automatically.

If a semantic index isn't available yet, the agent can still find context by using text search, file search, and language intelligence. Learn more about workspace indexing . Implicit context VS Code can use your editor activity to provide context to a request or suggest an attachment. This behavior depends on the interface, harness, and context settings: In the Chat view, the active file, notebook, or selected text can provide editor context.

With the Local harness The Local harness is deprecated and will be removed in February 2027. Move your workflows to the Copilot harness before then. Learn more about Local harness deprecation . and the default context settings, Ask includes active editor context. Agent suggests the active file as an attachment, while selected text is included. Agent Host sessions, such as Copilot, can also receive active editor context automatically.

The Local harness The Local harness is deprecated and will be removed in February 2027. Move your workflows to the Copilot harness before then. Learn more about Local harness deprecation . behavior doesn't apply to every agent. In the Agents window, implicit editor context is off by default. Add the files or project context that the request needs. A suggested attachment isn't the same as an explicitly attached file. When a source is important, add it to the request instead of assuming that an open editor supplies the full file.

The agent can also gather more context through its tools. Explicit context Add explicit context when you want to ensure that the agent considers a specific file, folder, symbol, image, web page, or other source. Type # in the chat input to choose a context item, or select Add Context ( + icon) in the chat input field. Explicit context guides the agent toward relevant information but still counts toward the context window.

Add only the sources that help the agent complete the current task. Learn more about adding context to chat . Tip Providing focused context up front might reduce the searches and file reads the agent needs, which can lower AI credit use. Avoid adding unrelated or large sources because they also consume context window tokens. Learn more about optimizing AI credit usage . Context window and compaction The context window is the maximum amount of information a language model can process in one request.

The user message, conversation history, instructions, referenced files, and tool outputs all use space in this window. When a conversation approaches the limit, VS Code automatically compacts older parts of the conversation into a summary. Compaction makes room for new information, but details from earlier messages might be summarized or omitted. You can also enter /compact in the c

The rest of this page is on the live site.