# AI Prompt Engineering Best Practices Guide > Comprehensive guide to writing effective prompts for Claude, GPT, and Gemini. Covers system prompts, few-shot learning, chain-of-thought, and structured output techniques. ## Install Paste the prompt below into your AI tool: ## Quick Use ### The 5 Rules of Effective Prompting 1. **Be specific** — "Write a Python function that validates email addresses using regex" beats "help me with email" 2. **Provide context** — Tell the model what role it plays, what the user needs, and what constraints apply 3. **Show examples** — Few-shot examples are worth 1000 words of instruction 4. **Structure output** — Specify the exact format you want (JSON, markdown, list) 5. **Iterate** — Test, refine, test again ## What is Prompt Engineering? Prompt engineering is the practice of crafting inputs to LLMs that produce reliable, high-quality outputs. As AI coding tools become central to development, prompt engineering is no longer optional — it directly impacts code quality, agent reliability, and development speed. This guide covers techniques that work across Claude, GPT, and Gemini. **Answer-Ready**: Prompt engineering guide for AI coding tools. Covers system prompts, few-shot learning, chain-of-thought, structured outputs, and role-based prompting. Techniques work across Claude, GPT, and Gemini. Essential for Claude Code CLAUDE.md, Cursor Rules, and agent development. **Best for**: Developers using AI coding tools who want better results. **Works with**: Claude Code, Cursor, Codex CLI, any LLM. ## Core Techniques ### 1. System Prompts (Role Setting) ``` You are a senior Python developer specializing in FastAPI. You write clean, typed, well-tested code. You prefer composition over inheritance. You always handle errors explicitly. ``` **Where to use**: CLAUDE.md (Claude Code), .cursorrules (Cursor), system message (API) ### 2. Few-Shot Learning ``` Convert natural language to SQL. Example 1: Input: "How many users signed up last month?" Output: SELECT COUNT(*) FROM users WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month') Example 2: Input: "Top 5 products by revenue" Output: SELECT product_name, SUM(price * quantity) as revenue FROM orders GROUP BY product_name ORDER BY revenue DESC LIMIT 5 Now convert: Input: "Average order value by country" ``` ### 3. Chain-of-Thought (CoT) ``` Analyze this code for security vulnerabilities. Think step by step: 1. First, identify all user inputs 2. Then, trace how each input flows through the code 3. Check if any input reaches a sensitive operation without validation 4. For each vulnerability found, classify severity (Critical/High/Medium/Low) 5. Suggest a specific fix for each vulnerability ``` ### 4. Structured Output Prompting ``` Extract the following from this error log and return as JSON: { "error_type": "string (e.g., TypeError, ConnectionError)", "root_cause": "string (one sentence)", "affected_files": ["list of file paths"], "suggested_fix": "string (specific code change)", "severity": "critical | high | medium | low" } ``` ### 5. Constraint-Based Prompting ``` Generate a React component with these constraints: - Use TypeScript with strict types - Use Tailwind CSS only (no inline styles, no CSS modules) - Must be a functional component with hooks - Must handle loading, error, and empty states - Must be accessible (ARIA labels, keyboard navigation) - Maximum 100 lines of code ``` ### 6. Negative Prompting (What NOT to Do) ``` Do NOT: - Add comments explaining obvious code - Create helper functions for one-time operations - Add error handling for impossible cases - Import libraries not already in package.json - Modify files outside the specified scope ``` ## Technique Selection Guide | Scenario | Best Technique | |----------|---------------| | Code generation | Constraints + Examples | | Bug fixing | Chain-of-thought | | Data extraction | Structured output + Examples | | Code review | Role setting + CoT | | Refactoring | Constraints + Negative prompting | | Documentation | Role setting + Examples | ## Platform-Specific Tips ### Claude Code (CLAUDE.md) ```markdown # CLAUDE.md - Use TypeScript strict mode - Run `npm test` after changes - Prefer composition over inheritance - Never modify files in /vendor/ ``` ### Cursor (.cursorrules) ``` You are an expert in Next.js 14, TypeScript, and Tailwind. Always use server components unless client interactivity is needed. ``` ### API (System Message) ```python response = client.messages.create( model="claude-sonnet-4-20250514", system="You are a code review expert. Be concise and specific.", messages=[{"role": "user", "content": code}], ) ``` ## FAQ **Q: Does prompt engineering still matter with advanced models?** A: Yes. Better prompts = more reliable outputs, fewer retries, lower costs. The gap between good and bad prompts is smaller with advanced models but still significant. **Q: How long should prompts be?** A: As short as possible, as long as necessary. A 5-line prompt with a good example beats a 50-line prompt without one. **Q: Should I use different prompts for different models?** A: Core techniques work across models. Adjust for model-specific features (e.g., Claude's tool use vs GPT's function calling). ## Source & Thanks > References: > - [Anthropic Prompt Engineering Guide](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering) > - [OpenAI Prompt Engineering Guide](https://platform.openai.com/docs/guides/prompt-engineering) > - [Google Gemini Prompting Guide](https://ai.google.dev/gemini-api/docs/prompting-intro) ## 快速使用 5 条提示词工程黄金法则:具体、提供上下文、给示例、指定格式、迭代优化。 ## 什么是提示词工程? 为 LLM 编写高效输入以获得可靠输出的实践。直接影响代码质量、Agent 可靠性和开发速度。 **一句话总结**:AI 编码工具提示词工程指南,系统提示 + Few-shot + 思维链 + 结构化输出 + 约束提示,适用于 Claude/GPT/Gemini,CLAUDE.md 和 Cursor Rules 必读。 ## 核心技巧 ### 1. 系统提示 设定角色和规则,写在 CLAUDE.md 或 .cursorrules。 ### 2. Few-Shot 学习 给 2-3 个输入输出示例比长篇指令更有效。 ### 3. 思维链 "逐步思考"提升复杂推理准确率。 ### 4. 结构化输出 指定 JSON/Markdown 格式确保可解析。 ### 5. 约束提示 告诉模型"不要做什么"和边界条件。 ## 常见问题 **Q: 高级模型还需要提示词工程?** A: 需要。好提示 = 更可靠 + 更少重试 + 更低成本。 **Q: 不同模型用不同提示?** A: 核心技巧通用,细节按模型特性调整。 ## 来源与致谢 > [Anthropic 指南](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering) | [OpenAI 指南](https://platform.openai.com/docs/guides/prompt-engineering) | [Google 指南](https://ai.google.dev/gemini-api/docs/prompting-intro) --- Source: https://tokrepo.com/en/workflows/15f82b68-ac1f-4e52-84b2-cead8b4cb869 Author: Skill Factory