Lompat ke konten

Pelajaran 7 ยท Chat and Codebase Context

Adding Context with @ Mentions

Give Cursor the right information with @ mentions, images and pasted code, watch the context window, trim noisy logs and decide when to start a fresh chat.

An AI model can only use what is in its context: the instructions, the conversation, the files it reads and the results of its tools. Good context is the difference between code that fits your project and generic code. Too much context takes space, may cost more and can bury what matters.

@ mentions

Type @ in the chat input to attach something specific. You can attach several items, typing @ again for each. As of December 2026, the documentation lists these kinds:

Mention What it attaches Example use
Files and folders A file, or a folder you can step into with / "Follow the pattern in this file"
Terminals Output from a terminal "Explain this failing build"
Chats A previous conversation "Continue the design we agreed on"
Git diffs Uncommitted changes, or your branch compared with main "Review what I changed"
Browser Context from the built-in browser "This button looks wrong"

The documented advice is simple: use mentions when you know which files matter. When you do not, let the agent search for itself.

Other ways to add context

  • Images: drag a file into the chat input, or paste a screenshot. This is useful for interface bugs and design mockups.
  • Selected code: add it from the editor, as in editing selected code.
  • Pasted logs: the shortcut list includes adding code or logs from the clipboard as context.
  • Voice: you can dictate instead of typing. Read the text before you send it.

Check screenshots and logs before attaching them. They can contain email addresses, tokens or customer names.

Choose context like an editor

Think about what a new teammate would need for this task. Usually that is:

  1. The file to change.
  2. One good example of the pattern to follow.
  3. The test file, or the command that proves success.
  4. The error message, complete and unchanged, if something fails.
TEXT
@api/users.py @tests/test_users.py
Add an endpoint that returns one user by id, following the style
of the list endpoint. Return 404 for unknown ids.
Add tests for both cases.

The context window

Each chat has a fixed context window. Files, tool runs and messages fill it as you work. When it gets close to full, the product summarizes older parts of the conversation to make room. A summary loses detail, so an early instruction may fade.

A ring next to the input shows how full the window is. Clicking it shows a breakdown, including the system prompt, tools, rules, skills, MCP, subagents, summarized history and conversation. If something unexpected takes a lot of room, you can act on it.

The stand-in below shows the idea of a budget. It estimates four characters per token, which is only a rough rule of thumb for English. Real token counts differ by model.

Python Stand-in: a context budget estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real windows are much larger, but the lesson holds: a huge log or an irrelevant file takes room from what matters. See context windows and token budgets for more.

Trim noisy logs

Send the part that explains the failure. This real script keeps error lines plus a few lines around them.

Python Keep only the useful part of a log

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not trim so hard that you remove the cause. Look at the first error, because later errors are often its consequences.

One task, one chat

Long chats collect old ideas, abandoned approaches and outdated file contents. The official troubleshooting guide advises starting a new chat when you finish a feature or switch tasks. When you start fresh, carry over a short brief:

TEXT
Goal: add CSV export to the orders page.
Done so far: export function in reports/csv_export.py, with tests.
Next: add the button and the route.
Constraint: no new dependencies.

You can also mention the earlier chat with @.

Signs of a context problem

Watch for these symptoms during a long task:

  • The agent repeats an approach that already failed.
  • It forgets a limit you stated early in the chat.
  • It edits based on an old version of a file.
  • Answers become vaguer as the chat grows.

Each can have other causes, but a fresh chat with a short, accurate brief is a cheap thing to try. Before you leave the old chat, ask for a handover note: the goal, the decisions made, the files changed and the open problems. Check the note against git diff, because it is a summary and may leave out or overstate things.

For knowledge that should survive every chat, such as your test command, do not rely on memory. Put it in a rule or in AGENTS.md, which you meet in project rules. For knowledge needed only for one task, a short plan file in the project can work well.

Common mistakes

  • Attaching whole folders "just in case".
  • Assuming a long chat still remembers a rule you gave at the start.
  • Pasting a screenshot that shows secrets.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. When is it best to use an @ mention?

    Mentions point at what you know is relevant; otherwise the agent can search.
  2. 2. What happens when a chat approaches the context limit?

    Summaries save space but lose detail, so restate important constraints.
  3. 3. Which part of a long log usually deserves a look first?

    Later errors often follow from the first one.

Summary

  • Use @ for files, folders, terminals, chats, Git diffs and the browser.
  • Give the file, an example, a check and the full error.
  • Watch the context ring, and trim noise.
  • Start a new chat for a new task, with a short brief.

Next: Codebase Search and Ignore Files

Progres Anda disimpan di browser ini. Tidak perlu akun.