Skip to content

Context management in Cursor is the choice of what the agent sees before it writes code: files, folders, code selections, indexed documentation, past chats and rules, attached with @ mentions or found by the agent’s own search. One canonical example file usually beats a long description, and too many attachments make the output worse, not better.

You ask the agent to add pagination to GET /api/users. It writes a new pagination helper from scratch, ignores the one in src/utils/pagination.ts, and uses offset/limit although the project moved to cursor-based pagination months ago. Nothing is wrong with the model. It never saw the helper, and nobody told it which pattern wins.

This page is for the developer in their first weeks with Cursor who wants the agent to follow the project’s existing patterns instead of inventing new ones. It sits in the quick-start path after MCP setup in Cursor and before your first feature.

What you get from managing Cursor’s context

Section titled “What you get from managing Cursor’s context”
  • A table of the context kinds Cursor’s agent accepts and when each one pays off.
  • Four copy-paste prompts: pattern reference, boundary fence, current library docs, and a clean handoff between chats.
  • A fixed order for adding context, so you attach only what the output proves you need.
  • A context receipt prompt and three terminal checks that show whether the agent reused the right code and stayed inside its fence, without reading every line of the diff.
  • A failure-mode table with the recovery for each symptom.

Which context can you give Cursor’s agent?

Section titled “Which context can you give Cursor’s agent?”

Type @ in the agent input to open the context menu, and filter by typing to find the item to attach. Picker labels have changed between Cursor releases, so trust the menu on your screen over any screenshot. The kinds of context below are the ones Cursor’s agent protocol carries, checked in @cursor/sdk 1.0.32 on 2026-09-26.

Context kindWhat the agent getsReach for it when
FileThe file you nameOne file holds the exact pattern to copy, or is the file to change
FolderA directory to work inThe agent needs the layout and naming of an area, not every file in it
Code selectionA selected function, class or blockOnly one function matters and the file is long
DocsAn indexed documentation sourceThe task depends on a library or internal API the model may know only from old training data
Past chatAn earlier conversationYou resume work whose decisions live in that chat
Terminal outputA terminal or a selection from itA failing test run or stack trace is the evidence
Git and pull requestsCommits, diffs and pull requestsThe task is “finish, review or explain this change”
RulesA rule you selectA convention should apply to this request even though its rule does not load automatically
ImagesA pasted screenshot or mockupA UI bug or a design is easier to show than to describe

Rules also add context without a mention: a rule set to always apply loads on every request. Keep those short and move the rest into file-scoped rules, as project rules in Cursor explains. Cursor’s docs say it also reads AGENTS.md in the project root and subdirectories, and Cursor’s help says it reads CLAUDE.md files the same way and always applies them to every conversation. Cursor’s help also says rules apply to Agent only, not to Tab completion, Inline Edit or Bugbot PR reviews (both checked 2026-10-12). Every attachment costs tokens on every turn of the chat; token management in Cursor covers the bill.

Point at one canonical example instead of describing the pattern

Section titled “Point at one canonical example instead of describing the pattern”

A description of your conventions leaves room for interpretation. A file that already follows them does not. Compare two prompts for the same task:

Weak: Create an orders endpoint. We use Express with auth middleware,
zod for validation and our own error format.
Strong: Create POST /api/orders following @src/routes/users.ts exactly:
same middleware order, same zod validation, same AppError handling,
same response shape.

The strong prompt gives the agent something to copy and something to be checked against. Cross-cutting conventions fail the same way: ask for error handling without context and the agent wraps the handler in try/catch and returns { error: "Something went wrong" }, while your codebase already has an AppError class, a central error-handler middleware and a response format with error codes. Attach the middleware and the class, and the agent throws AppError like every other route. Pick the example deliberately: the newest file that follows the current convention, not the oldest file in the directory. If two files disagree, the agent may follow either one.

After you paste a prompt, check that each @ reference shows up as an attached item. If a path stays plain text, delete it, type @ again and pick the file from the menu.

Use a folder mention when the agent needs a map

Section titled “Use a folder mention when the agent needs a map”

Attach a folder when the question is “where does this go and what is it called”, not “copy this function”. For example: Look at @src/components/ and add a NotificationBanner component that follows the same file layout, naming and story file as the existing components. A folder is cheaper than attaching every file in it, and the agent can open the files it needs.

Attach only the function as a code selection when the rest of a long file is noise: Refactor calculateDiscount (attached) to support percentage and fixed-amount discounts. Keep the signature. Attaching the whole file also works; it costs more tokens and gives the agent more to misread.

Section titled “When should you attach context and when should you let the agent search?”

Cursor’s agent can search the codebase, read files and run commands on its own. Attaching context is a way to remove guesswork, not a requirement for every request.

SituationAttach it yourselfLet the agent search
You know which file holds the patternYes, one file—
You know which file will changeYes—
You do not know where the code lives—Yes, and ask it to report what it found
The task touches every caller of a functionAttach the functionLet it find the callers
An earlier chat made the design decisionAttach the past chat or a handoff note—
The task is broad and exploratory—Yes, in Plan Mode first

The hybrid works best for most real tasks: attach the file to change and the rule that governs it, and ask the agent to find the rest. For example: @src/services/billing/subscription.ts Add a three-retry strategy for failed renewal payments. Search the codebase for how we already retry external calls and reuse that helper; tell me which one you picked.

Add context in a fixed order, and only when the output proves you need it

Section titled “Add context in a fixed order, and only when the output proves you need it”

Attaching ten files “just in case” makes the agent weigh irrelevant code as heavily as the one file that matters. Start small and let each wrong answer tell you what was missing.

  1. Task only. Write the task and the acceptance check (“the new test passes, npm run typecheck is clean”). Let the agent search.

  2. Add the canonical example if the output uses the wrong pattern: one file, attached with @.

  3. Add the governing rule if it still misses a convention that a rule already states, for example the error-handling rule.

  4. Add a folder if it puts files in the wrong place or names them wrongly.

  5. Add docs if it calls an API that does not exist in your library version.

  6. Start a new chat if the third correction in a row does not stick. The history now carries the wrong attempts, and they compete with your corrections.

When a step fixes the output for good, write that context into a rule or the canonical example, so the next person does not repeat the ladder.

Positive context tells the agent what to copy. Boundary context tells it what not to touch. Name both, and name the files by path so you can check the fence afterwards.

Exclusions such as “no caching, no logging” matter as much as the file list: a vague task invites the agent to add abstractions you then have to review and delete. The last line matters too. Without it, an agent that hits a boundary tends to find a creative way around it, which is the change you least want to discover in review.

Give the agent current library documentation

Section titled “Give the agent current library documentation”

Models learn libraries from training data, which lags the release you run. Two ways close the gap in Cursor.

  • Indexed docs. Cursor’s docs context attaches a documentation source Cursor has indexed, including sites you add by URL. The Docs entry in Cursor Settings manages indexed sources; its location has moved between releases, so search Settings for “Docs”. Use it for internal API docs and for libraries you work with every day.
  • Context7 MCP server. Context7 fetches version-specific docs for a named library on request (npm @upstash/context7-mcp 4.1.1, checked 2026-09-26). Add the remote server to your global ~/.cursor/mcp.json; the API key is optional and belongs in the global file, never in the repository. Context7 for coding agents covers keys, library pinning and the skills mode.
{
"mcpServers": {
"context7": { "url": "https://mcp.context7.com/mcp" }
}
}

If you have a key, add it as a header on the same entry:

"context7": {
"url": "https://mcp.context7.com/mcp",
"headers": { "Authorization": "Bearer YOUR_API_KEY" }
}

Replace YOUR_API_KEY with your Context7 key; the file lives in your home directory, so the key never enters the repository.

Keep files out of the agent’s reach with .cursorignore

Section titled “Keep files out of the agent’s reach with .cursorignore”

Generated code, snapshots, vendored libraries and large fixtures pull the agent’s search towards files nobody should copy. .cursorignore uses .gitignore syntax at the repository root, and Cursor’s agent code loads both .gitignore and .cursorignore (checked in @cursor/sdk 1.0.32, 2026-09-26), but only .cursorignore is the one to use for keeping files away from the agent. When the agent searches a path this way blocks, the tool returns Path '…' is filtered out by .cursorignore (checked in @cursor/sdk 1.0.32). Build the file from your repository’s real file counts, as tuning Cursor for large codebases shows.

Carry context across chats without dragging the whole history

Section titled “Carry context across chats without dragging the whole history”

Every message and tool result stays in the chat, so a long chat carries old attempts, dead ends and outdated plans. When the chat fills the model’s context window, Cursor compacts it into a summary of its own choosing. Start a new chat when you finish a task, switch areas of the codebase, or notice the agent repeating a mistake you already corrected.

You have two ways to bring the useful part along. A past chat reference attaches the earlier conversation as it was. A short handoff note that you write, or ask the agent to write, keeps only what you choose, and you can review it in 30 seconds. Prefer the note for anything that must survive: decisions, constraints and the next step.

Then open a new chat and send: Read @docs/handoff/notifications.md and continue with the step under Open. Delete the note once the new chat has picked up the work, unless your team wants such notes in review; do not add its folder to .cursorignore, because the next chat has to read it. Token management in Cursor shows how to log each compaction with a preCompact hook, so you see which chats run too long.

How do you prove the agent used the right context?

Section titled “How do you prove the agent used the right context?”

Right context shows up in the result: the new code imports the existing helper, touches only the fenced files, and passes the project’s gates. You can check all three without reading the diff line by line.

1. Ask for a context receipt before the code. The receipt turns an invisible choice into a list you can compare with what you expected.

If the receipt names a new helper where you know one exists, attach the existing one and ask again. That costs one message; finding it in review costs a rewrite.

2. Check reuse and scope in the terminal. After the agent finishes and before you commit, run checks like these for the pagination example. For that task, the fence was src/routes/users.ts and tests/routes/users.test.ts:

Terminal window
# Every file the agent created or changed, including new files
git status --porcelain -uall
# Files outside the fence you set in the prompt: expect no output
{ git diff --name-only HEAD; git ls-files --others --exclude-standard; } \
| grep -Ev '^(src/routes/users\.ts|tests/routes/users\.test\.ts)$'
# The route reuses the existing helper: expect one import line
rg -n "utils/pagination" src/routes/users.ts
# No second pagination helper appeared anywhere else: expect no output
rg -n "export (default )?(async )?(function|const) \w*[Pp]aginat" src --glob '!src/utils/pagination.ts'

3. Run the gates the project already trusts. Tests, the type check and the linter prove behaviour; the checks above prove the agent built on the right code. When both pass, the developer who ran the chat signs off and names the context receipt in the pull request description, so the reviewer checks one list instead of re-deriving it. Your first feature puts this into a full loop with tests first.

What breaks in Cursor context management, and how to recover

Section titled “What breaks in Cursor context management, and how to recover”
SymptomLikely causeRecovery
The agent writes a new helper next to an existing oneIt never saw the existing oneAttach the helper file, ask for a context receipt, and delete the duplicate with the reuse check above
The agent follows an outdated patternYour example file, or an older file it found, uses the old patternAttach the newest file that follows the current convention and name the old one as “do not copy”
Files land in the wrong directoryIt does not know the layoutAttach the target folder, and add a file-scoped rule if it happens twice
Code calls an API that does not exist in your versionIt relied on training dataAdd indexed docs or use Context7, and ask it to cite the doc page for each API
Docs context returns outdated pagesThe indexed source is staleRemove the source in the Docs entry of Cursor Settings and add the URL again
The output adds caching, logging or layers nobody asked forThe task was vague about scopeAdd explicit exclusions to the boundaries and ask for the smallest change that passes the test
The agent ignores a file you attachedThe file is long and the relevant part is buriedAttach the one function as a code selection and quote its name in the prompt
A search returns Path '…' is filtered out by .cursorignoreThe path matches a .cursorignore patternRemove the pattern if the agent needs the file, or attach a non-ignored copy of the relevant part
A rule and the example file disagreeTwo sources of truthFix whichever is wrong; until then, say in the prompt which one wins
Corrections stop sticking after a long chatHistory full of old attempts, or a compaction dropped your instructionWrite a handoff note and continue in a new chat
Ten attached files, worse outputThe agent weighs irrelevant codeCut to the two or three files that matter and let it search for the rest

Using Claude Code or Codex as well? The same ideas apply with different mechanics: see Claude Code’s memory system and context patterns in Codex.

Did this guide help you?

One tap, no sign-in.

not at all a lot