Skip to main content

Session Management

Prerequisites

  • Completed Modules 1-2
  • Copilot CLI installed and authenticated

Learning Objectives

  • Understand session lifecycle and persistence
  • Resume and continue previous sessions
  • Use session-related slash commands
  • Track session usage and metrics
  • Clear context when needed

Concepts

What is a Session?

A session is a continuous interaction with Copilot CLI that maintains:

  • Conversation history - All prompts and responses
  • Context - Files, directories, and information gathered
  • Tool approvals - Permissions granted during the session
  • Working directory - The directory scope

Sessions can be:

  • Continued - Pick up where you left off
  • Resumed - Restore a previous session
  • Cleared - Start fresh while staying in Copilot

Session Storage

Sessions are stored in your Copilot config directory:

  • Default: ~/.copilot/
  • Custom: Set via XDG_CONFIG_HOME environment variable

Hands-On Exercises

Exercise 1: Session Persistence

note

How Session Persistence Works:

  • Each copilot invocation starts a fresh session by default
  • To restore a previous session, use /resume slash command or --resume flag
  • Key session commands available:
    • /resume - Switch to a different session (optionally specify session ID)
    • /rename - Rename the current session (alias for /session rename)
    • /context - Show context window token usage and visualization
    • /usage - Display session usage metrics and statistics
    • /session - Show session info and workspace summary (use subcommands for details)
    • /compact - Summarize conversation history to reduce context window usage
    • /share - Share session to markdown file or GitHub gist

Goal: Understand how sessions persist between interactions.

Steps:

  1. Start a new session and gather some context:

    copilot
  2. Ask Copilot to remember something:

    Remember that I'm working on a user authentication feature. The main file is auth.py.
  3. Ask about the files:

    What files are in the current directory?
  4. Exit with Ctrl+C (not /exit).

  5. Immediately start Copilot again:

    copilot
  6. Ask if it remembers:

    What feature was I working on?
  7. Copilot should remember from the previous session context.

Expected Outcome: Recent session context is preserved when quickly re-entering.

Exercise 2: Resume a Session

Goal: Learn to explicitly resume a previous session.

Steps:

  1. Start a session and do some work:

    copilot
    Create a file called notes.txt with "Session 1 notes"
  2. Use /session to see session info:

    /session
  3. Note the session ID displayed.

  4. Exit completely:

    /exit
  5. Start a new session with resume flag:

    copilot --resume
  6. Verify you're in the same context:

    What was the last file we created?

Expected Outcome: The --resume flag restores your previous session state.

Exercise 3: Slash Commands for Session Control

Goal: Master session-related slash commands.

Steps:

  1. Start an interactive session:

    copilot
  2. View all commands:

    /help
  3. Check current session:

    /session

    This shows:

    • Session ID
    • Start time
    • Duration
    • Files modified
    • Commands executed
  4. Check usage statistics:

    /usage

    This shows:

    • Token consumption
    • API calls made
    • Model used
  5. View current working directory:

    /cwd
  6. Clear conversation history:

    /clear
  7. Verify context is cleared:

    What was I just working on?

    Copilot won't know because context was cleared.

Expected Outcome: You understand each session command's purpose.

Exercise 4: Managing Working Directory

Goal: Control which directories Copilot can access.

Steps:

  1. Start in your home directory:

    cd ~
    copilot
  2. Check current working directory:

    /cwd
  3. Change to a specific project:

    /cwd ~/copilot-workshop
  4. Verify the change:

    /cwd
  5. Add another directory for access:

    /add-dir ~/another-project
  6. List all accessible directories:

    /list-dirs
  7. Try to access a file outside allowed directories:

    Show me /etc/passwd

    Copilot should request permission or refuse.

Expected Outcome: You can control Copilot's file access scope.

Exercise 5: Session Clearing Strategies

Goal: Know when and how to clear session context.

Steps:

  1. Build up context in a session:

    copilot
    Let's work on the frontend. The main component is App.tsx.
    The backend uses Express.js with routes in /api.
    We're using PostgreSQL for the database.
  2. Check token usage:

    /context
  3. Notice context filling up. If working on something unrelated:

    /clear
  4. Start fresh:

    Now let's work on a completely different Python script.
  5. Alternative: Partial clear - Exit and start new:

    /exit
    copilot

When to Clear:

ScenarioAction
Switching to unrelated task/clear
Confused responses/clear
Context limit approaching/compact first, then /clear if needed
Sensitive info discussed/clear and /exit
Session becomes slow/clear

Expected Outcome: You know when clearing context improves your workflow.

Exercise 6: Multiple Sessions Strategy

Goal: Work with multiple Copilot instances.

Steps:

  1. Open Terminal 1 - Frontend work:

    cd ~/project/frontend
    copilot
    Let's focus on React components
  2. Open Terminal 2 - Backend work:

    cd ~/project/backend
    copilot
    Let's focus on API endpoints
  3. Each terminal maintains its own:

    • Session context
    • Working directory
    • Tool approvals
  4. Switch between terminals as needed for parallel work.

Expected Outcome: You can run multiple focused sessions simultaneously.

Exercise 7: Session Export and Sharing

Goal: Export session transcripts for documentation or sharing.

Steps:

  1. Start a session and do meaningful work:

    copilot
    Explain the architecture of a typical Express.js application
  2. Have a productive conversation building up knowledge.

  3. Export to a file:

    copilot --share ./session-export.md
  4. Or export to a GitHub Gist:

    copilot --share-gist
  5. Review the exported markdown file:

    cat session-export.md

Expected Outcome: Session transcript saved for future reference or sharing.

Session Slash Commands Reference

CommandDescriptionExample
/helpList all commands/help
/sessionShow session info/session
/usageShow usage stats/usage
/cwdShow/change directory/cwd ~/project
/add-dirAdd accessible directory/add-dir /tmp
/list-dirsList accessible directories/list-dirs
/clearClear conversation history/clear
/exitEnd session/exit
/modelSwitch AI model/model gpt-4

Command Line Flags

FlagDescription
--resumeResume last session
--share PATHExport to markdown file
--share-gistExport to GitHub Gist
--silentSuppress stats/logs

Summary

  • ✅ Sessions maintain conversation history and context
  • ✅ Use --resume to continue previous sessions
  • /clear resets context without exiting
  • /cwd and /add-dir control file access scope
  • ✅ Run multiple sessions in different terminals
  • ✅ Export sessions with --share for documentation

Next Steps

→ Continue to Module 4: Custom Instructions

References