Home › LLM apps & agents › Writing LLM prompts in code rules

Writing LLM prompts in code rules

Cursor rules for writing and maintaining prompts inside applications: structure, variables, structured output, and evals.

CursorLLM apps & agents

How to use

Save this as .cursor/rules/prompts-in-code.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/prompts-in-code.mdcDownload  
---
description: Use when creating or editing prompts, system messages, or LLM calls in application code.
alwaysApply: false
---

# Prompts as code

- Prompts live in dedicated files (`prompts/<name>.md` or `prompts/<name>.ts`), not inline string literals scattered in handlers. Each has a name and version.
- Structure system prompts in clear sections: role and goal, context, rules/constraints, output format, examples. Put stable instructions first and variable data last (better for prompt caching).
- Insert variables with a template function and clear delimiters (XML-style tags like `<document>...</document>`), and escape or strip delimiter look-alikes from user input.
- Be specific and positive: say what to do ("Answer in 3 bullet points citing [doc-id]") rather than only what to avoid. State what to do when information is missing.
- For machine-read output, use the provider's structured output / tool schema and validate with zod/Pydantic; don't regex free text.
- Few-shot examples should cover edge cases and be realistic; keep them short and consistent with the required format.
- Untrusted content (user input, retrieved docs, web pages) goes in delimited sections with an instruction that it's data, not instructions.
- Model, temperature, and max tokens come from config next to the prompt.
- Every prompt change updates or adds eval cases (inputs + expected properties) and reports before/after results.
- Log prompt version with each call for debugging.

## Don't
- Don't tell the model to "be accurate" as a substitute for giving it the needed context or tools.

More: all LLM apps & agents rules · all Cursor rules

Related rules

Claude Agent SDK CLAUDE.md

CLAUDE.md for building agents with the Claude Agent SDK (Python/TypeScript): options, tools, permissions, and hooks.

Claude Code (CLAUDE.md)LLM apps & agents

CrewAI multi-agent AGENTS.md

AGENTS.md for CrewAI 1.x projects: YAML agents and tasks, crews and flows, tools, and cost-aware runs.

AGENTS.mdLLM apps & agents

LangGraph agent rules

Cursor rules for building agents with LangGraph in Python: typed state, nodes, tools, checkpointers, and tests.

CursorLLM apps & agents

LLM app (Python) AGENTS.md

AGENTS.md for Python apps that call LLM APIs (OpenAI, Anthropic, Gemini): prompts, structured output, evals, and cost.

AGENTS.mdLLM apps & agents

OpenAI Agents SDK rules

Cursor rules for the OpenAI Agents SDK (Python 0.2x): agents, function tools, handoffs, guardrails, sessions, and tracing.

CursorLLM apps & agents

Vercel AI SDK rules

Cursor rules for the Vercel AI SDK 5 in TypeScript: streamText, structured output, tools, and useChat UIs.

CursorLLM apps & agents

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.