---
name: prompt-engineering
description: Write, refactor, and harden production prompts and structured outputs for current LLMs, covering system prompt structure, few-shot examples, delimiting untrusted input, native structured outputs with Pydantic / JSON Schema, schema design rules, validation and repair, tool definitions, prompt versioning, prompt caching, and model migration. Includes a schema linter script for structured-output pitfalls. Use when writing or reviewing a prompt, when model output is unreliable or unparseable, when designing a schema or tool interface for an LLM, or when moving prompts to a new model.
license: MIT
metadata:
  author: kaushik-holla
  version: "1.0"
title: prompt-engineering
canonical_url: https://skilld.dev/gh/kaushik-holla/agent-skills/prompt-engineering
last_updated: 2026-11-22T18:46:02.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [assets/example_schema.json](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/assets/example_schema.json), [references/prompt-patterns.md](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/references/prompt-patterns.md), [scripts/schema_lint.py](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/scripts/schema_lint.py).
>
> If the user asked to install this Skill, run `npx skilld install kaushik-holla/agent-skills/prompt-engineering`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/prompt-engineering`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/kaushik-holla/agent-skills/prompt-engineering) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/prompt-engineering`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/prompt-engineering --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/prompt-engineering --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Prompt Engineering and Structured Outputs

Prompts are code that runs on a probabilistic interpreter. Treat them like code: explicit contracts, typed outputs, version control, and tests (evals). Most "the model is bad at this" problems are underspecified prompts or schemas.

## When To Use

- Writing a new system prompt, classifier, extractor, router, or agent instruction set.
- Output is inconsistent, unparseable, or ignores instructions.
- Designing the Pydantic / JSON Schema for a structured response or a tool's arguments.
- Migrating prompts to a new model snapshot or provider.
- Reviewing a prompt for injection risk or hidden assumptions.

## Principles

1. **Be explicit about the goal, audience, and what "good" looks like.** Modern models follow instructions precisely; vague prompts get generic output.
2. **Give the reason behind constraints.** "Don't use markdown, because this is read aloud by a TTS engine" generalizes better than "NO MARKDOWN."
3. **Show, don't adjectivize.** Two or three diverse examples beat "be concise, professional, accurate."
4. **Separate instructions from data.** Wrap untrusted content (user text, documents, tool results) in clear delimiters (XML-style tags work well across providers) and say it is data.
5. **Say what to do, not only what to avoid.** Replace "don't hallucinate" with "if the answer isn't in the sources, reply 'unknown'."
6. **Structure long prompts.** Long reference material first, instructions and the question last.
7. **Let reasoning models reason.** Don't script step-by-step chains for models with built-in thinking; set the reasoning effort / budget instead and specify the output.
8. **Enforce structure with the API, not with pleading.** Use native structured outputs instead of "respond ONLY with valid JSON."

Templates for each pattern (system prompt, classification, extraction, tool-using agent, rewrites, model migration): [references/prompt-patterns.md](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/references/prompt-patterns.md).

## Workflow

### 1. Write the contract first

Before the prompt, write: inputs, output schema, 3-6 success criteria, known hard cases, and what should happen on out-of-scope input. This doubles as the eval spec for the `llm-evals` skill.

### 2. Draft the prompt

Use the skeleton in [references/prompt-patterns.md](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/references/prompt-patterns.md): Role, Context, Task, Constraints, Output, Examples. Keep static sections stable and at the top (prompt caching); put per-request content at the end.

### 3. Define the structured output

```python
from typing import Literal

from pydantic import BaseModel, Field


class TicketTriage(BaseModel):
    """Triage decision for one support ticket."""

    reasoning: str = Field(description="1-2 sentences quoting the words that justify the category.")
    category: Literal["billing", "bug", "feature_request", "account", "other"]
    urgency: Literal["low", "medium", "high"] = Field(
        description="high only if the customer is blocked or data is at risk."
    )
    product_area: str | None = Field(description="Product area if explicitly mentioned, else null.")
```

Call it through native structured outputs:

```python
# LangChain (any provider): returns a validated TicketTriage instance
from langchain.chat_models import init_chat_model

llm = init_chat_model(settings.llm_model, temperature=0)
triage = llm.with_structured_output(TicketTriage).invoke(messages)

# OpenAI SDK, Responses API
resp = client.responses.parse(model=settings.llm_model, input=messages, text_format=TicketTriage)
triage = resp.output_parsed
```

Anthropic, Google, and most hosted providers offer equivalent schema-constrained output or tool-use-based extraction; `with_structured_output` picks the provider's native method. Instructor and PydanticAI are good alternatives outside LangChain.

### 4. Lint the schema

```bash
python scripts/schema_lint.py --model myapp.schemas:TicketTriage --strict
python scripts/schema_lint.py schema.json --fail-on warn        # CI
python scripts/schema_lint.py assets/example_schema.json --strict   # demo: a deliberately flawed schema
```

[scripts/schema_lint.py](https://skilld.dev/api/skills-raw/kaushik-holla/agent-skills/prompt-engineering/scripts/schema_lint.py) flags deep nesting, missing descriptions, free-form objects, non-required fields under strict mode, oversized enums and unions, unbounded arrays, and verdict-before-reasoning order. SDK helpers (`responses.parse`, `with_structured_output(..., strict=True)`) often patch `additionalProperties` and `required` for you; the linter reports what the raw schema says, so treat those two findings as "verify the SDK handles it."

Schema design rules:

- **Flat and small.** 3-7 fields per call. Split complex extractions into multiple calls or steps.
- **Every field has a description**, written as an instruction ("ISO 8601 date of the invoice, not the due date").
- **`Literal` / enums for anything a router or metric consumes.**
- **Nullable instead of optional** in strict modes (`str | None` and listed in `required`), with "null when absent" in the description.
- **Reasoning before verdict** in field order, so the model commits to evidence first.
- **Bound arrays** (`max_length`) and avoid free-form `dict[str, Any]` fields.
- **Version schemas** alongside prompts; a renamed field is a breaking change for downstream code.

### 5. Validate and repair

- Validate every response with Pydantic even when the provider "guarantees" the schema (refusals, truncation at max tokens, and provider bugs happen).
- On validation failure: retry once with the validation error appended, then fall back to a safe default and log the raw output. Never loop unbounded.
- Check `finish_reason` / stop reason: a truncated response is a bug in your token budget, not in the parser.

### 6. Design tools like APIs for a new hire

- Names are `verb_noun` (`search_orders`, `get_invoice`), descriptions say **when** to use the tool and when not to.
- Few tools with clear boundaries beat many overlapping ones; overlapping tools cause wrong-tool calls.
- Arguments are typed, described, and constrained (enums, ranges). Return concise, model-readable results with IDs the model can pass to the next call.
- Errors are returned as actionable text ("No order 1234. Order IDs look like ORD-123456.") so the model can recover.

### 7. Version and test

- Store prompts in files (or a prompt registry), not inline f-strings scattered through code. Include a `prompt_version` in trace metadata.
- Every prompt change runs the eval set (`llm-evals`) and is compared against the previous version.
- Render templates with explicit escaping; never interpolate untrusted text into the instruction section.

## Checklist

- [ ] Goal, audience, and success criteria stated in the prompt
- [ ] Constraints carry reasons; out-of-scope behavior is specified
- [ ] Untrusted input delimited and declared as data
- [ ] 2-3 diverse examples, including a hard or edge case
- [ ] Native structured output with a linted, flat, described schema
- [ ] Validation + single repair retry + safe fallback; truncation detected
- [ ] Static prefix stable for caching; per-request content last
- [ ] Prompt versioned, traced, and covered by evals

## Common Mistakes

- "Respond ONLY with JSON" instead of schema-enforced output.
- ALL-CAPS and repeated warnings carried over from older models; newer models over-apply them and become rigid.
- One giant prompt doing classification, extraction, and generation at once.
- Optional fields without "null when absent," which invites fabricated values.
- Putting the verdict field before the reasoning field.
- Asking a reasoning model to "think step by step" while also capping its reasoning budget at minimum.
- Changing a prompt without re-running evals because "it's just wording."

## References

- OpenAI, Structured Outputs guide: <https://platform.openai.com/docs/guides/structured-outputs>
- Anthropic, prompt engineering overview: <https://docs.claude.com/en/docs/build-with-claude/prompt-engineering/overview>
- LangChain, structured output: <https://docs.langchain.com/oss/python/langchain/structured-output>
- Pydantic, JSON Schema: <https://docs.pydantic.dev/latest/concepts/json_schema/>
- OWASP LLM01:2025 Prompt Injection: <https://genai.owasp.org/llmrisk/llm01-prompt-injection/>
