Skip to content

Agent and Chat Optimization: Tips 61-75

You open the agent panel, type “add user authentication,” and watch Cursor create a half-baked auth module that ignores your existing middleware, uses a different ORM than your project, and breaks three test files. You spend more time fixing the mess than it would have taken to write it yourself. The problem is not agent mode — it is that agent mode is a power tool, and power tools without technique cause damage. These 15 tips teach you the technique.

  • The test-driven agent workflow that produces code you can actually trust
  • Context management strategies that stop the agent from going off-track mid-conversation
  • Prompt patterns for the most common agent tasks (features, refactors, debugging, migrations)
  • Clear rules for when to babysit the agent versus when to let it run unsupervised

Tip 61: Always Write Tests First with Agent Mode

Section titled “Tip 61: Always Write Tests First with Agent Mode”

The single most impactful change you can make to your agent workflow: tell it to write tests before implementation. This gives the agent a clear definition of “done” that it can verify autonomously.

With YOLO mode enabled (Tip 5), the agent:

  1. Creates the test file with well-defined assertions
  2. Creates the implementation file
  3. Runs vitest
  4. Sees failures, fixes the code
  5. Runs vitest again
  6. Repeats until all tests pass

You did not approve a single command. You did not review intermediate output. And the final code has test coverage by definition.

Tip 62: Use @ References to Ground Every Conversation

Section titled “Tip 62: Use @ References to Ground Every Conversation”

The @ symbol is not optional — it is the difference between the agent guessing at your code structure and knowing it. Always include relevant context:

@src/lib/db/schema.ts @src/lib/db/queries.ts
Add a new "organizations" table with name, slug, and owner_id fields.
Create the Drizzle schema, migration, and CRUD query functions following
the same pattern as the existing user queries in the referenced files.

Without the @ references, the agent might generate raw SQL instead of Drizzle, use a different naming convention, or create files in the wrong directory. With them, it pattern-matches against your existing code and produces consistent output.

Tip 63: Start New Conversations for New Tasks

Section titled “Tip 63: Start New Conversations for New Tasks”

Agent conversations have a limited context window, and long conversations degrade quality. Here are the signs you need a new conversation:

  • The agent starts “forgetting” instructions you gave earlier
  • It repeats mistakes you already corrected
  • It references files that are no longer relevant
  • Its responses are getting slower

Start a new conversation with Cmd+N in the agent panel. Carry over only the essential context: the files you need, the specific task, and any constraints. Do not try to resume a stale conversation by pasting a summary — it is faster and more reliable to start fresh.

Tip 64: Use Ask Mode to Plan, Agent Mode to Execute

Section titled “Tip 64: Use Ask Mode to Plan, Agent Mode to Execute”

The two-step workflow produces dramatically better results than a single agent conversation:

  1. Open Ask mode: “I need to add Stripe subscription billing. Look at @src/api/ and @src/lib/ and suggest an implementation plan. List the files that need to change, the new files to create, and the order of operations. Do not make any changes.”
  2. Review the plan: The agent produces a structured plan without touching your code
  3. Open a new Agent conversation: “Implement step 1 from this plan: [paste the relevant section]. Follow the patterns in @src/api/users/route.ts for the endpoint structure.”
  4. Repeat: One agent conversation per step, each starting fresh with focused context

This approach prevents the agent from taking on too much at once. Each conversation is focused, the context is fresh, and you can verify correctness between steps.

Every file you reference with @ consumes context tokens. Reference too many files and the agent’s reasoning quality drops because it cannot hold everything in working memory. The rules:

  • 1-3 files: Agent can reason about every line in detail
  • 4-8 files: Agent understands overall structure but may miss specifics
  • 8+ files: Agent skims and may hallucinate connections between files

For tasks that require many files, break them into sub-tasks that each reference only the relevant subset:

# Instead of:
@src/api/ @src/lib/ @src/components/ @src/types/ "Refactor auth"
# Do:
# Conversation 1: @src/lib/auth.ts @src/types/user.ts "Refactor the auth service"
# Conversation 2: @src/api/auth/ @src/lib/auth.ts "Update API routes for new auth service"
# Conversation 3: @src/components/AuthProvider.tsx @src/lib/auth.ts "Update the frontend auth provider"

Tip 66: Use @web for Up-to-Date Library Documentation

Section titled “Tip 66: Use @web for Up-to-Date Library Documentation”

When working with a library that has been updated since the AI’s training data, use @web to fetch current docs:

This is especially valuable for rapidly evolving libraries (Next.js App Router, Drizzle, tRPC) where the AI’s built-in knowledge may be a version or two behind.

Tip 67: Reference @git for Change-Aware Prompts

Section titled “Tip 67: Reference @git for Change-Aware Prompts”

The @git context lets the agent understand what has changed recently:

@git diff main
Review the changes I have made since branching from main. Check for:
1. Missing error handling in new code
2. Type safety issues
3. Consistency with existing patterns
4. Missing test coverage for new functionality

This is a fast pre-PR review that catches issues before your teammates see the code.

Tip 68: Use the “Implement and Verify” Pattern

Section titled “Tip 68: Use the “Implement and Verify” Pattern”

For any non-trivial feature, structure your prompt to include a verification step:

The “after implementation” section gives the agent a clear verification checklist. With YOLO mode, it runs these checks automatically.

When the agent cannot find a bug through static analysis alone, use this iterative debugging pattern:

  1. Tell the agent: “Add console.log statements at every decision point in @src/services/payment.ts to trace the execution flow. Log function entry/exit, parameter values, and intermediate results.”
  2. Run the failing scenario and copy the log output
  3. Paste the logs back into the agent conversation: “Here is the log output. Analyze the trace and identify where the behavior diverges from expected.”
  4. The agent now has runtime data to combine with static code analysis
  5. It proposes a targeted fix based on the actual execution path

This is dramatically more effective than telling the agent “fix the bug” with no runtime context.

Tip 70: Use the “Incremental Migration” Pattern

Section titled “Tip 70: Use the “Incremental Migration” Pattern”

For large migrations (library upgrades, pattern changes, framework migrations), do not ask the agent to migrate everything at once. Use incremental migration:

After the first file is verified, move to the next: “Now convert @src/api/users.ts following the same pattern established in the health endpoint.” Each step builds on the last, and you verify before proceeding.

End your development sessions by having the agent clean up everything in one pass:

Tip 72: Let the Agent Generate from Error Logs

Section titled “Tip 72: Let the Agent Generate from Error Logs”

Instead of describing a production issue, paste the actual error:

Here is the error from our production logs:
TypeError: Cannot read properties of undefined (reading 'email')
at processWebhook (src/api/webhooks/stripe.ts:47:28)
at handleRequest (src/middleware/router.ts:112:5)
The webhook payload from Stripe sometimes does not include customer details
when it is a subscription update event. Fix the handler in
@src/api/webhooks/stripe.ts to handle missing customer data gracefully.

Real stack traces give the agent exact file paths, line numbers, and error types — far more useful than a description.

Tip 73: Combine Agent Mode with Background Agents

Section titled “Tip 73: Combine Agent Mode with Background Agents”

For long-running tasks, start a background agent and continue working in a regular agent conversation:

  • Background agent: “Generate comprehensive tests for every service in @src/services/. Aim for 90% coverage.”
  • Foreground agent: Continue implementing the feature you are currently working on

The background agent creates a PR when it is done. Review the PR, merge the tests, and your test coverage is improved without blocking your active work.

Save frequently used prompts as .md files in your project and reference them with @:

.cursor/prompts/new-endpoint.md
Create a new API endpoint following these conventions:
- Use the route pattern from @src/api/users/route.ts
- Include zod validation for request body
- Add proper error handling with our AppError class
- Return standardized JSON responses
- Add vitest tests in the adjacent __tests__ directory
- Run tests after implementation

Then in the agent: “@.cursor/prompts/new-endpoint.md — create a POST /api/organizations endpoint that creates a new organization with name and slug fields.”

The agent is not always right, and sometimes it goes down a dead-end path. Signs you should stop, restore a checkpoint, and restart:

  • The agent has made the same mistake three times in a row
  • The agent is generating code that contradicts your instructions
  • The conversation has exceeded 15-20 exchanges without converging on a solution
  • The agent is editing files you explicitly told it not to touch

When you restart, add the failure context to your new prompt: “Previous attempt failed because the agent tried to use raw SQL instead of Drizzle ORM. Ensure all database operations use the Drizzle query builder from @src/lib/db/queries.ts.”

Agent modifies files you did not intend: This usually happens when your prompt is ambiguous about scope. Be explicit: “Only modify files in @src/api/users/. Do not touch any other directories.” If it already happened, use checkpoints to restore.

Agent goes into an infinite fix loop: The agent runs tests, sees a failure, “fixes” it, introduces a new failure, “fixes” that, and loops indefinitely. Stop the agent, review what went wrong, and restart with a more constrained prompt. Often the issue is that the agent is fighting its own previous changes.

Agent produces working code that does not match your conventions: This happens when project rules are missing or incomplete. Invest time in your .cursorrules or .cursor/rules/ (Tips 9-11). The 30 minutes you spend on rules saves hours of rework.

Context overflow in long conversations: If the agent’s responses become increasingly generic or start ignoring your instructions, the context window is saturated. Start a new conversation. There is no way to “trim” context in an existing conversation.

Agent mode handles individual features and focused tasks well. But what happens when your project has 500,000 lines of code and the agent needs to understand architectural boundaries, dependency chains, and module boundaries? Move to Large Codebase Strategies (Tips 76-90) for enterprise-scale techniques.