Skip to content

Keep context lean

A conversation accumulates messages, tool outputs, and file contents. Every new request re-sends that history as input, so an unrelated old topic keeps costing tokens without improving answers.

The context window is the maximum the model can consider in one request (input and output share it). It is a hard limit and a soft one: models use information in the middle of long contexts less reliably than at the start or end (Lost in the Middle), so quality can degrade well before the window is full. Cost and quality both argue for as little context as the task needs, but enough.

VS Code Chat context window control showing token usage by category VS Code Chat context picker for adding relevant context
Monitor context usage (left) and add relevant context (right). Sources: VS Code Docs — Add context to chat and Optimize AI credit usage.
SituationAction
Switching to an unrelated taskStart a new chat session (Ctrl+N / ⌘N).
Want to try an alternative or ask a side questionFork the conversation (/fork, or hover a message → Fork Conversation) instead of re-explaining.
Long session you still needRun /compact (optionally /compact focus on the API design decisions) to summarize older turns.
You know the relevant code#-mention the file, folder, or symbol rather than letting the agent search broadly.
You need codebase search#codebase runs a search; it does not add the whole codebase to context.
Repeated grep + view loops slow down tasks and bloat historyInstall Code Context (cctx) from the VS Code Marketplace so Copilot uses single-call, block-clamped #cctxNativeContext lookups (<8 KiB inline budget; 15.20× faster warm retrieval and 24.7% faster task completion, as reported).
Noisy search results (logs, build output, generated files)Exclude them: .gitignore (index + agent search), files.exclude (hide entirely), search.exclude (hide from agent search only).
Web referenceUse #fetch with a URL only when needed; fetched pages enter the context.
Independent researchDelegate to a subagent so the exploration stays out of the main conversation.

Search-match snippets count toward the context window even when the agent never opens the file, so excluding noisy paths helps directly.

A subagent keeps intermediate exploration out of the parent chat’s context window, but its first turn has its own system-prompt and tool-schema prefix. As a practical heuristic, handle short lookups (up to 5 tool calls) directly; delegate broad or multi-file exploration when its intermediate results would otherwise bloat later parent turns. Measure both cost and task success: isolation is not free.

Repeat the baseline task from Measure your usage after the change and compare session credits and the context-window token count. Confirm the answer quality is unchanged.

  • /compact is lossy: details you still need may be summarized away. Add focus instructions, and say what must survive (decisions, constraints, file paths, open items).
  • Compacting changes the start of the history, so the provider’s prompt cache for that prefix is rebuilt. Compact once at a natural pause rather than repeatedly; for a short remaining task, finishing it is often cheaper.
  • If a long session is dominated by big tool results (file reads, command output), trimming those (or starting a new chat with a short hand-off note) usually helps more than summarizing the conversation.
  • Starting a new chat loses history; put durable facts into custom instructions so you do not re-explain them.
  • Attach the smallest set of authoritative context that lets the task succeed — not everything available.

Ordered by venue tier, then citations. Findings are hypotheses to verify on your own workload; see the full list in Papers.