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_HOMEenvironment variable
Hands-On Exercises
Exercise 1: Session Persistence
How Session Persistence Works:
- Each
copilotinvocation starts a fresh session by default - To restore a previous session, use
/resumeslash command or--resumeflag - 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:
-
Start a new session and gather some context:
copilot -
Ask Copilot to remember something:
Remember that I'm working on a user authentication feature. The main file is auth.py. -
Ask about the files:
What files are in the current directory? -
Exit with
Ctrl+C(not/exit). -
Immediately start Copilot again:
copilot -
Ask if it remembers:
What feature was I working on? -
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:
-
Start a session and do some work:
copilotCreate a file called notes.txt with "Session 1 notes" -
Use
/sessionto see session info:/session -
Note the session ID displayed.
-
Exit completely:
/exit -
Start a new session with resume flag:
copilot --resume -
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:
-
Start an interactive session:
copilot -
View all commands:
/help -
Check current session:
/sessionThis shows:
- Session ID
- Start time
- Duration
- Files modified
- Commands executed
-
Check usage statistics:
/usageThis shows:
- Token consumption
- API calls made
- Model used
-
View current working directory:
/cwd -
Clear conversation history:
/clear -
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:
-
Start in your home directory:
cd ~
copilot -
Check current working directory:
/cwd -
Change to a specific project:
/cwd ~/copilot-workshop -
Verify the change:
/cwd -
Add another directory for access:
/add-dir ~/another-project -
List all accessible directories:
/list-dirs -
Try to access a file outside allowed directories:
Show me /etc/passwdCopilot 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:
-
Build up context in a session:
copilotLet'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. -
Check token usage:
/context -
Notice context filling up. If working on something unrelated:
/clear -
Start fresh:
Now let's work on a completely different Python script. -
Alternative: Partial clear - Exit and start new:
/exitcopilot
When to Clear:
| Scenario | Action |
|---|---|
| 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:
-
Open Terminal 1 - Frontend work:
cd ~/project/frontend
copilotLet's focus on React components -
Open Terminal 2 - Backend work:
cd ~/project/backend
copilotLet's focus on API endpoints -
Each terminal maintains its own:
- Session context
- Working directory
- Tool approvals
-
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:
-
Start a session and do meaningful work:
copilotExplain the architecture of a typical Express.js application -
Have a productive conversation building up knowledge.
-
Export to a file:
copilot --share ./session-export.md -
Or export to a GitHub Gist:
copilot --share-gist -
Review the exported markdown file:
cat session-export.md
Expected Outcome: Session transcript saved for future reference or sharing.
Session Slash Commands Reference
| Command | Description | Example |
|---|---|---|
/help | List all commands | /help |
/session | Show session info | /session |
/usage | Show usage stats | /usage |
/cwd | Show/change directory | /cwd ~/project |
/add-dir | Add accessible directory | /add-dir /tmp |
/list-dirs | List accessible directories | /list-dirs |
/clear | Clear conversation history | /clear |
/exit | End session | /exit |
/model | Switch AI model | /model gpt-4 |
Command Line Flags
| Flag | Description |
|---|---|
--resume | Resume last session |
--share PATH | Export to markdown file |
--share-gist | Export to GitHub Gist |
--silent | Suppress stats/logs |
Summary
- ✅ Sessions maintain conversation history and context
- ✅ Use
--resumeto continue previous sessions - ✅
/clearresets context without exiting - ✅
/cwdand/add-dircontrol file access scope - ✅ Run multiple sessions in different terminals
- ✅ Export sessions with
--sharefor documentation
Next Steps
→ Continue to Module 4: Custom Instructions