is now available! Read about the new features and fixes from November.

Manage chat sessions in VS Code

Use chat in Visual Studio Code to have conversation-based AI interactions. A chat session consists of the sequence of prompts and responses between you and the AI, along with any relevant context from your code or files. This article describes how to create and manage chat sessions, export chat sessions, and how to view the chat session history.

What is a chat session?

A chat session is the history of your interactions with the AI within a single conversation and includes all prompts, responses, and context used during that conversation. Each chat session maintains its own history, allowing you to ask follow-up questions or refine your requests based on previous interactions.

As you interact with the AI, the chat session accumulates context from your prompts and responses (context window). When you create a new chat session, the previous conversation history is cleared, and a fresh context window is established for the new session.

Use checkpoints to roll back to a previous state within a chat session or edit a previous prompt to modify the course of the conversation. Learn more about checkpoints and editing chat requests.

You can create multiple chat sessions to organize your conversations by topic or task. Each session is independent, so context from one session does not carry over to another. VS Code maintains the history of your chat sessions, allowing you to return to previous conversations at any time.

If you're using agents, the session can run locally on your machine, in the background using a CLI, or in the cloud. Learn more about agents.

Tip

Start a new chat session when you want to change topics to help the AI provide more relevant responses.

Start a new chat session

VS Code supports different types of sessions (local, background, and cloud) and different views (side bar, editor tab, and separate window) for chat sessions. Learn more about background and cloud agent sessions.

Open a local chat session in the side bar.

  • In Chat view, New Chat (+) > New Chat
  • ⌘N (Windows, Linux Ctrl+N)
  • Chat: New Chat command
Screenshot of opening a new chat session in the Chat view in VS Code.
Open local chat as an editor tab.

  • In Chat view, New Chat (+) > New Chat Editor
  • Chat: New Chat Editor command
Screenshot of opening a new chat session in an editor tab in VS Code.
Open local chat in a separate window.

  • In Chat view, New Chat (+) > New Chat Window
  • ⌘N (Windows, Linux Ctrl+N)
  • Chat: New Chat Window command
Screenshot  of opening a new chat session in a separate window in VS Code.
Open a background agent session.

  • In Chat view, New Chat (+) > New Background Agent
  • Chat: New Background Agent command
Screenshot of opening a new background agent session in the Chat view in VS Code.
Open a cloud agent session.

  • In Chat view, New Chat (+) > New Cloud Agent
  • Chat: New Cloud Agent command
Screenshot of opening a new cloud agent session in the Chat view in VS Code.

Move a chat session to a different view

You can move an existing chat session to a different view, such as from the Chat view to an editor tab or a separate window. This is useful when you want to change how you interact with the chat session or organize your workspace differently.

  • In the Chat view, select the ... icon and then select Move Chat into Editor Area or Move Chat into New Window.

  • In an chat editor, select the ... icon in the top-right corner of the editor tab, and then select Move Chat into Secondary Side Bar.

  • In a chat window, select the Move Chat into Secondary Side Bar button in the window title bar.

  • Use the following commands from the Command Palette:

    • Chat: Move Chat into Editor Area

    • Chat: Move Chat into New Window

    • Chat: Move Chat into Side Bar

Chat session history

When you create a new chat session, the previous conversation history is cleared. However, all your chat sessions are saved in the session history, allowing you to return to previous conversations and continue where you left off. This is useful when you want to review past interactions, reference previous responses, or resume a conversation from an earlier session.

Screenshot of an agent session in VS Code showing code changes and chat interaction.

By default, when you start a new chat session, the Chat view shows your most recent sessions, whether they run locally, in the background, or in the cloud. Select Show All Sessions to view the full history of sessions, allowing you to search and filter the list. The list is scoped to your current workspace. If you don't have a workspace open, the list shows all sessions across your workspaces.

Learn more about agents and viewing and managing agent sessions.

Tip

Make the Chat view wider to automatically switch to side-by-side mode or use the toggle button in the top-right corner of the Chat view.

Save and export chat sessions

You can save chat sessions to preserve important conversations or reuse them later for similar tasks.

Export a chat session as a JSON file

You can export a chat session to save it for later reference or share it with others. Exporting a chat session creates a JSON file that contains all prompts and responses from the session.

To export a chat session:

  1. Open the chat session you want to export in the Chat view.

  2. Run the Chat: Export Chat... command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

  3. Choose a location to save the JSON file.

Alternatively, you can copy individual prompts or responses to the clipboard by right-clicking the message and selecting Copy. To copy the entire chat session in Markdown format, right-click the Chat view and select Copy All.

Save a chat session as a reusable prompt

You can save a chat session as a reusable prompt to reuse for similar tasks.

To save a chat session as a reusable prompt:

  1. Open the chat session you want to save in the Chat view.

  2. Type /savePrompt in the chat input box and press Enter.

    The command creates a .prompt.md file that generalizes your current chat conversation into a reusable prompt. The prompt file has placeholders where appropriate.

  3. Review and edit the generated prompt file as needed, then save it to your workspace.

Context-isolated subagents

A subagent enables you to delegate tasks to an isolated, autonomous agent within your chat session. Subagents operate independently from the main chat session and have their own context window. This is useful to optimize context management for complex multi-step tasks like research or analysis.

Subagents don't run asynchronously or in the background, however, they operate autonomously without pausing for user feedback. When a subagent completes its task, it returns only the final result to the main chat session, keeping the main context window focused on the primary conversation.

Subagents use the same agent and have access to the same tools available to the main chat session, except for creating other subagents. They also use the same AI model as the main chat session.

Invoke a subagent

To invoke a subagent in a prompt:

  1. Enable the runSubagent tool in the tool picker

    If you use a custom prompt file or custom agent, ensure you specify runSubagent in the tools frontmatter property.

  2. In the chat prompt, ask to use a subagent to perform a task.

    The following examples illustrate how to invoke a subagent:

    • Use a subagent to research the best authentication methods for web applications. Summarize the findings.
    • Run #runSubagent to research the user's task comprehensively using read-only tools. Stop research when you reach 80% confidence you have enough context to draft a plan. Return this context.

Use a custom agent with subagents (Experimental)

By default, a subagent inherits the agent from the main chat session. You can configure a subagent to use a different built-in or custom agent. For example, if you want to run a research task as part of a development task, you can start the research task in a subagent that uses a research agent.

To run a subagent with a specific agent:

  1. Enable support for custom agents in subagents with the chat.customAgentInSubagent.enabled setting

  2. Make sure the custom agent doesn't have its infer property set to false, as this prevents the agent from being used in subagents.

  3. Prompt the AI about which agents are available for use as subagents. Your custom agent should be listed.

    Which subagents can you use?
    
  4. Prompt the AI to use a custom or built-in agent for the subagent. For example:

    • Run the research agent as a subagent to research the best auth methods for this project.
    • Use the plan agent in a subagent to create an implementation plan for myfeature. Then save the plan in plans/myfeature.plan.md
Note

To prevent a custom agent from being used as a subagent, set the metadata property infer to false in the *.agent.md file.

Navigate between prompts in a chat session

Use the following keyboard shortcuts to navigate between prompts in a chat session:

  • ⌥⌘↑ (Windows, Linux Ctrl+Alt+Up): Go to the previous prompt in the chat session.
  • ⌥⌘↓ (Windows, Linux Ctrl+Alt+Down): Go to the next prompt in the chat session.
  • ⌥⌘PageUp (Windows, Linux Ctrl+Alt+PageUp): Go to the previous code block in the chat session.
  • ⌥⌘PageDown (Windows, Linux Ctrl+Alt+PageDown): Go to the next code block in the chat session.

Tips for managing chat sessions

Consider the following tips to help you work effectively with chat sessions:

  • Start a new session for different topics: start a new chat session to avoid carrying over context from unrelated conversations. This helps you get more relevant responses.

  • Use editor tabs for side-by-side comparisons: open multiple chat sessions as editor tabs to compare different approaches or solutions side-by-side.

  • Use separate windows for multi-monitor setups: open chat in a separate window on a secondary monitor to keep it visible while you work on code in the main window.

  • Background tasks with remote agents: use remote coding agents to perform AI tasks in the background while you continue working in VS Code.

  • Interactive agent sessions: use local agent sessions for interactive tasks that require real-time input and feedback.