AI/ML5 min read

Prompt Engineering for Applications

Engineer production prompts: system prompt design, output formatting, few-shot examples, prompt chaining, and testing prompts systematically.

prompt-engineeringllmaiproduction

Prompt engineering for applications differs from conversational use - you need consistent, structured outputs, predictable behavior, and systematic testing. Production prompts require versioning, evaluation, and monitoring.

Treat prompts as code - version control them, test them, and measure them. Specify output format explicitly (JSON schema). Few-shot examples outperform instructions alone. Test prompts against a diverse eval set before deploying. Monitor outputs in production.

Workflow

  1. Design effective system promptsThe system prompt sets context, constraints, and output expectations - the foundation of reliable behavior.
  2. Format outputs reliablyParsing free-text LLM output is brittle - JSON outputs are reliable and type-safe.
  3. Write few-shot examplesFew-shot examples demonstrate the pattern - models learn from examples more reliably than from instructions.
  4. Chain prompts for complex tasksComplex tasks are more reliably solved by a sequence of focused prompts than one monolithic prompt.
  5. Test and evaluate promptsWithout evaluation, prompt changes are shots in the dark - you can't tell if you're improving.

Output Format

System prompt (role + constraints + format + examples), JSON schema for structured output validated with Zod, 3-5 few-shot examples, prompt chain with intermediate validation, promptfoo eval set for regression testing.

Examples

Structured output system: `const systemPrompt = `You are a text classifier. Classify the sentiment of the given text as positive, negative, or neutral. You MUST respond with valid JSON matching this schema exactly: {"sentiment": "positive"|"negative"|"neutral", "confidence": 0.0-1.0, "reasoning": "string"}. No other text.`;`
Chain: `async function analyzeComplaint(text: string) { const issue = await identifyIssue(text); if (!issue.isComplaint) return { action: "acknowledge" }; const severity = await classifySeverity(text, issue.category); return severity.score > 0.8 ? await escalate(text, issue, severity) : await generateResponse(text, issue) }`
Eval: `// promptfoo.config.yaml: prompts: [./prompts/classifier.txt]; providers: [openai:gpt-4o]; tests: - vars: {text: "The product was broken"} assert: [{type: llm-rubric, value: "response should indicate negative sentiment"}]`

Edge Cases & Gotchas

  • Prompt injection: if user input is included in your prompt, attackers can inject instructions that override your system prompt. Sanitize: escape special characters, use `<user_input>` delimiters, and test with adversarial inputs like "Ignore previous instructions and..."
  • Model version differences: a prompt tuned for GPT-4o may behave differently on GPT-4o-mini. Maintain separate eval sets per model. When upgrading models, re-run all evals before deploying
  • Latency vs quality: longer, more detailed prompts get better outputs but cost more tokens and take longer. Profile prompt token count and completion tokens separately. Use prompt compression (remove redundant text) when latency is critical

When to Break These Rules

  • When a task has deterministic logic (calculation, formatting, lookup), use code instead of LLMs - it's cheaper, faster, and perfectly reliable. Use LLMs only for tasks requiring language understanding or generation.

Reference Files

  • Anthropic prompt engineering: https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview
  • OpenAI structured outputs: https://platform.openai.com/docs/guides/structured-outputs
  • promptfoo: https://promptfoo.dev/
  • LangSmith for evals: https://docs.smith.langchain.com/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.