Multi-File Context

45 min intermediate Lesson 4

Learning Outcomes

  • Use @-mentions to reference files and symbols in Chat
  • Leverage Agent's context search for project-wide questions and refactoring
  • Reference external documentation with @docs
  • Manage context window usage effectively
  • Understand what Cursor indexes and how to control it
NOTE
Tool Version
This lesson reflects Cursor v3.x (mid-2026). Context handling has changed significantly — Agent now self-gathers context automatically. Some older @-mentions (@codebase, @web, @definitions) were removed in v2.0.

Lesson Plan

Segment Duration Topic
Intro 3 min Why context matters for AI quality
Demo 1 8 min @file and @folder — targeted context
Demo 2 8 min Agent's automatic context gathering
Explain 5 min How Agent finds relevant code
Demo 3 8 min @Docs, @Terminals, and other context sources
Explain 5 min Context window management strategies
Demo 4 5 min Choosing the right context for the task
Wrap-up 3 min Key takeaways and next lesson preview

Before You Begin

Pre-work:

Shopping List:

  • A project with at least 5-10 files (a real project works best)
  • AI side pane open and ready (Cmd+L / Ctrl+L)

1 @-Mentioning Files and Symbols

The @ symbol in Cursor's chat is your tool for giving the AI precise context. Instead of the AI guessing which files matter, you tell it explicitly.

Referencing a file:

In the Chat input, type @ and start typing a filename:

@src/utils/helpers.js Can you explain the debounce function in this file?

As you type after @, Cursor shows an autocomplete dropdown listing matching files from your project. Select the file you want and it appears as a context chip above the input.

Referencing a symbol (function, class, variable):

You can reference specific code symbols directly:

@calculateShipping I need to add international shipping rates to this function.
What parameters should I add?

Cursor will find the calculateShipping function across your codebase and attach it as context.

Multiple references in one message:

@src/models/Order.js @src/services/payment.js @src/routes/checkout.js
I want to add a discount code feature. Show me how these files need to change.

Each referenced file gets its full content included in the AI's context.

What the context bar shows:

Above the Chat input, you'll see pills/chips for each attached context item:

  • File names with their path
  • Symbol names with their location
  • An X button to remove each item

Auto-detected context:

Beyond explicit @-mentions, Cursor also includes:

  • The currently active file in the editor
  • Any code you've selected before opening Chat
  • Recent error messages from the terminal (if relevant)
TIP
Tip
Use @-mentions for precision. If you know exactly which 2-3 files are relevant, mention them explicitly rather than relying on Agent's context search. This gives faster, more focused responses.
WARNING
Watch Out
Each @-mentioned file consumes context window tokens. A 500-line file might use 2,000-4,000 tokens. If your files are large, be selective about which ones you attach.

2 Agent's Automatic Context Gathering

When you use Agent mode, Cursor automatically finds relevant code across your project — you don't need to manually attach context. Just ask your question naturally.

How it works:

How is user authentication implemented across this project?

When you ask Agent a question, Cursor:

  1. Automatically searches your project for relevant files and code sections
  2. Identifies the most relevant snippets using semantic search
  3. Reads files it needs for deeper understanding
  4. Answers based on this project-wide context

Good project-wide questions for Agent:

Question Why It Works
"How does the app handle errors?" Agent searches error handling patterns across files
"Where is the database connection configured?" Finds config files and connection code
"What API endpoints exist?" Scans route definitions across the project
"Are there any TODO comments in the project?" Searches for comment patterns
"What testing framework does this project use?" Looks at test files and config

Automatic context vs explicit @file — when to add manually:

Scenario Approach
You know the exact file matters Add @filename explicitly
You need 2-3 specific files together @file1 @file2 @file3
You're exploring the project Just ask — Agent finds what it needs
Question spans many files Just ask — Agent gathers broadly
You need the AI to read a full file @filename (ensures complete content)
General question about patterns Just ask — Agent picks relevant snippets

When Agent might miss context:

  • Very large monorepos may have some files missed
  • If your question is ambiguous, Agent may find the wrong files — be specific
  • For full file content (not snippets), explicitly attach with @filename
  • Newly created files may not yet be discoverable
NOTE
How It Works
In older Cursor versions, you needed to type @codebase to trigger project-wide search. This is no longer needed — Agent mode automatically gathers context using semantic search tools. You can still use @file for explicit attachments when you want to ensure specific files are included.

3 @docs for Documentation References

Cursor can reference external documentation to help with framework-specific questions.

Using @docs:

@docs Next.js How do I set up API routes with the App Router?

The @docs prefix tells Cursor to pull in documentation from known sources for the specified technology.

Supported documentation sources:

Cursor maintains indexes of popular framework and library documentation. Common examples:

  • @docs React — React documentation
  • @docs Next.js — Next.js framework docs
  • @docs Python — Python standard library
  • @docs TypeScript — TypeScript handbook
  • @docs Tailwind — Tailwind CSS utilities

Adding custom documentation sources:

You can add your own documentation URLs in Cursor Settings:

  1. Open Cursor Settings (Cursor Settings (gear icon))
  2. Navigate to Features → Docs
  3. Click "Add new doc"
  4. Enter the documentation URL
  5. Cursor will crawl and index it
  1. Open Cursor Settings (Ctrl+Shift+J)
  2. Navigate to Features → Docs
  3. Click "Add new doc"
  4. Enter the documentation URL
  5. Cursor will crawl and index it

Practical examples:

@docs Express @src/routes/api.js
How do I add middleware that validates JWT tokens before these routes?

This combines documentation context (Express best practices) with your actual code for a highly relevant answer.

@docs PostgreSQL @src/db/schema.sql
I need to add a full-text search index for the articles table. What's the best approach?

Other @ context sources:

Syntax What It Does
`` Searches the web for current information
@git References git history and diffs
`` Includes type definitions and interfaces
@folder References all files in a specific folder
TIP
Tip
Combine @docs with @file for the best results. The documentation provides the 'how it should work' and your file provides 'how it currently works'. The AI bridges the gap.
WARNING
Watch Out
Not all documentation sources are pre-indexed. If @docs doesn't return useful results for your technology, you may need to add the documentation URL manually in settings.

4 Context Window Management

Every piece of context you provide to the AI consumes space in the model's context window. Managing this budget effectively is crucial for good answers.

Understanding the context window:

Model Approximate Context Window Practical Code Limit
Claude Opus 5 1M tokens ~600K tokens for code
Claude Sonnet 5 1M tokens ~600K tokens for code
Claude Haiku 4.5 200K tokens ~150K tokens for code

Figures current at August 2026, and Cursor's model list changes often — check the picker rather than this table. Note the gap between the two columns: the practical limit is well under the advertised one, because recall across a long context degrades before the context is full. Treat the window as storage, not as dependable working memory.

A "token" is roughly 3-4 characters of code. A typical 100-line code file uses about 1,000-2,000 tokens.

What consumes context:

Context Source Token Cost
Each @file reference Full file size (can be 1K-10K+ tokens)
Agent's context search results Selected snippets (usually 2K-5K tokens)
@docs results Relevant doc sections (1K-3K tokens)
Conversation history All previous messages (cumulative)
System prompt ~500-1K tokens (automatic)
Your current message Your text (usually small)

Signs you've exceeded useful context:

  • AI responses become vague or generic
  • The AI "forgets" something you mentioned earlier in the conversation
  • Responses focus on only one of the files you referenced
  • You see a warning about context limits in the chat

Context management strategies:

  1. Start specific, broaden if needed — begin with 1-2 @file references, add more only if the AI needs them

  2. Start fresh for new topics — click the + button for a new chat instead of continuing an unrelated conversation

  3. Remove unnecessary context — click X on context chips you no longer need

  4. Use Agent's context search for exploration, then @file for action — ask Agent's context search to find the right files, then start a new chat with those specific files

  5. Reference small sections — select just the relevant function before pressing Cmd+L, rather than attaching the entire file

NOTE
How It Works
When context exceeds the model's window, Cursor truncates older conversation history. Your most recent messages and explicitly attached files are preserved, but earlier discussion may be summarized or dropped.

5 Selecting the Right Context for the Task

Different tasks require different context strategies. Here's a framework for choosing:

Task: Understanding unfamiliar code

Context: Agent's context search (broad exploration)
Prompt: "How does the payment processing flow work in this project?"

Follow up with specific files once you know where to look:

Context: @src/services/stripe.js @src/routes/payment.js
Prompt: "Walk me through the checkout flow step by step"

Task: Adding a new feature

Context: @file with similar feature + @file where you'll add it
Prompt: "@src/routes/users.js @src/routes/orders.js
I want to add a new route for managing wishlists.
Follow the same pattern as the orders route."

Task: Fixing a bug

Context: The file with the bug + related files
Prompt: "@src/utils/dateFormat.js @src/components/Calendar.jsx
The calendar shows dates one day off. The issue seems to be
in the date formatting utility. Can you spot the timezone bug?"

Task: Refactoring across multiple files

Context: All affected files (but keep it under 5-6 files)
Prompt: "@src/api/v1/routes.js @src/api/v1/controllers.js
@src/api/v1/middleware.js
I want to refactor from Express to Fastify. What changes are needed?"

Task: Writing documentation

Context: The code + any existing docs
Prompt: "@src/lib/cache.js @README.md
Add JSDoc comments to all exported functions in cache.js
and update the README's API section."

The "minimum effective context" principle:

Always ask yourself: "What's the minimum context the AI needs to answer this well?" More context isn't always better — it can dilute focus and slow responses.

TIP
Tip
If you're unsure what context to provide, start with your question and no @ references. If the AI asks for more information or gives a generic answer, add context and re-ask.

6 Understanding What Cursor Indexes

The Agent's context search feature relies on Cursor's code index. Understanding what's indexed helps you get better results.

What gets indexed:

  • All source code files in your workspace (.js, .ts, .py, .java, etc.)
  • Configuration files (package.json, tsconfig.json, etc.)
  • Markdown files and documentation
  • Test files
  • Scripts and build files

What does NOT get indexed:

  • Files in .gitignore (by default)
  • Binary files (images, compiled code, archives)
  • node_modules/ and other dependency directories
  • Very large generated files (e.g., bundled output, minified code)
  • Files exceeding the individual file size limit

Checking indexing status:

  1. Look at the status bar — a spinning icon means indexing is in progress
  2. Open the Command Palette (Cmd+Shift+P) and run "Cursor: Index Status" to see details
  3. In Settings (Cursor Settings (gear icon)) → Features → Codebase Indexing, you can see the total indexed files
  1. Look at the status bar — a spinning icon means indexing is in progress
  2. Open the Command Palette (Ctrl+Shift+P) and run "Cursor: Index Status" to see details
  3. In Settings (Ctrl+Shift+J) → Features → Codebase Indexing, you can see the total indexed files

Controlling what's indexed:

You can create a .cursorignore file in your project root (similar to .gitignore) to exclude files from indexing:

# .cursorignore
build/
dist/
coverage/
*.min.js
*.generated.ts
vendor/

This is useful when:

  • Generated files confuse the AI (it references outdated generated code)
  • Large data files slow down indexing
  • You have folders that aren't relevant to your AI queries

Re-indexing:

If you've made significant structural changes to your project (renamed folders, added many files), you may want to force a re-index:

  1. Open Command Palette
  2. Run "Cursor: Reindex Codebase"
  3. Wait for the status bar indicator to show completion

Index freshness:

Cursor re-indexes files automatically when:

  • You save a modified file
  • New files are created
  • Files are deleted or renamed
  • You pull new changes from git

The index stays up-to-date for most workflows without manual intervention.

NOTE
How It Works
Cursor uses embedding-based indexing. Each chunk of code is converted into a numerical vector that captures its semantic meaning. When you query Agent's context search, your question is also embedded and compared against all code chunks to find the best matches.
WARNING
Watch Out
If you just cloned a large repository, give Cursor a few minutes to complete initial indexing before relying on Agent's context search. The status bar will show when it's done.

Questions & Answers

Q: Can I use Agent's context search on very large projects (100K+ files)?
Cursor can index large projects, but there are practical limits. For monorepos or very large codebases, consider opening only the relevant sub-folder as your workspace. This keeps the index focused and responses faster. You can also use .cursorignore to exclude irrelevant sections.
Q: Does @file include the entire file content, even for large files?
Yes — @file includes the complete file. If a file is 5,000 lines, all of it goes into context. For very large files, consider selecting just the relevant section in the editor and pressing Cmd+L to add only that selection as context.
Q: Can the AI access files outside my workspace folder?
No. Cursor only has access to files within the opened workspace folder. If you need the AI to see files from another project, either open both in a multi-root workspace or copy the relevant content into chat manually.
Q: How accurate is Agent's context search at finding the right files?
It's semantic search — it works well for conceptual questions ("how does auth work?") but may miss files with unusual naming. If Agent's context search misses a file you expected, reference it directly with @filename. Over time, you'll learn which queries work well with Agent's context search and which need explicit references.

Key Takeaways

  1. @file for precision — when you know exactly which files are relevant, reference them explicitly
  2. Agent's context search for exploration — when you don't know where something lives, let the index find it
  3. @docs for frameworks — combine official documentation with your code for best-practice answers
  4. Context has a budget — every reference costs tokens. Use the minimum effective context.
  5. Index awareness — know what's indexed (code files) and what's not (node_modules, binaries)
  6. .cursorignore — exclude irrelevant files to keep the index clean and the AI focused

Next Steps: In Lesson 5 — Debugging with Cursor, you'll learn how to use AI to interpret errors, diagnose bugs, and iterate toward fixes.