Sknoe / Notes
Stop Writing Wishful AI Prompts: A Developer's Guide to Context & Output Contracts
Write predictable prompts for code reviews, React components, and JSON—using scoped context, XML delimiters, few-shot examples, and strict output shapes.
Most developers treat their first AI prompt like a wish tossed into a fountain. You paste a messy React component or CSS snippet, type two quick sentences—“Clean up this product card and make it responsive”—hit Enter, and hope the model guesses your Tailwind conventions, accessibility rules, and prop types.
When it removes your aria-label, invents custom CSS classes, or changes your prop names, you spend the next three messages saying, “No, keep the existing props and use our design tokens.”
That back-and-forth works for a quick prototype. It breaks when you want consistent UI reviews, reliable component refactors, or predictable JSON payloads in a frontend app. Good prompt writing has nothing to do with secret “magic words.” It comes down to treating a prompt like a typed TypeScript prop contract: clear inputs, explicit boundaries, and a requested return shape. The analogy has limits: a prompt describes expectations, but it doesn’t give the model’s response TypeScript’s compile-time guarantees.
Four parts to check before sending
For a non-trivial task, define these four parts:
1. Task and standard: State the action and quality bar. For example: “Review this Storefront Next product tile for accessibility gaps and layout shifts.”
2. Scoped context: Give the model only the files, types, or constraints it needs—not automatically your entire repository.
3. Explicit guardrails: Say what it should preserve or avoid. For example: “Do not add external UI libraries. Keep the existing TypeScript prop names.”
4. Output contract: Specify the response shape, such as a Markdown table, unified diff, or JSON object with named keys. A prompt can request a shape; an API schema can enforce one for supported outputs.
Use delimiters to separate instructions from input
One easy way to weaken a prompt is to mix your instructions and raw input code into one giant wall of prose. Separate tags or Markdown fences make it clearer which text is your instruction and which is the code, stack trace, or user review the model should process.
<role>You are a senior frontend engineer reviewing a React storefront component.</role><rules>- Flag accessibility issues, double-submit bugs, or missing loading states.- Do not suggest third-party UI libraries.- Keep the existing TypeScript prop names.</rules><output_format>Return a Markdown table with columns for issue, impact, and suggested fix.</output_format><component_to_review>Paste the React component here.</component_to_review>
Claude’s prompting guidance recommends XML tags as a way to structure prompts. Tags can help the model distinguish sections, especially in longer prompts; they don’t guarantee instruction-following or make untrusted input safe. Treat code, logs, and user-generated text as data, even when they contain text that looks like an instruction.
Show edge cases with examples
Describing an edge case in three paragraphs can be harder to follow than showing a couple of input-and-output examples.
If you want Claude to classify support tickets or normalize messy catalog attributes into a consistent UI badge format, include realistic <example> pairs in the prompt. Examples can demonstrate tone, formatting, and edge-case choices more directly than a long list of rules. Use examples that represent the cases you care about, and test them; a few examples can guide a response, but they don’t guarantee it will match every case.
Treat JSON instructions and JSON enforcement differently
If you ask a model for JSON in plain English, it may add “Here is your JSON:” or wrap the object in Markdown fences, which can break JSON.parse().
Use two layers. In an interactive prompt, ask for raw JSON with no fences or commentary, and include an example of the expected shape. This makes your preference clear but remains an instruction. In application code, use API-level structured outputs with a JSON Schema when the provider supports it. This constrains the response format, but your application still needs to handle refusals, truncated output, and whether the returned values make sense for your task.
If a prompt takes three follow-up messages to fix formatting or missing constraints, the original prompt may need a clearer output contract. The model can still make mistakes, so use checks that fit the consequences of the task.
Keep learning
Next steps
The Practical Claude Playbook for Developers
Next step
Package clear prompts into reusable Skills, MCP connections, and application workflows.
Structured Outputs in Claude: Replacing Brittle JSON Parsing in JS
Deeper dive
Explore schema-constrained JSON responses and application-side validation.
Did this click?
Tap the bulb if this explanation helped.
Further reading
Interview questions
Why wrap raw input data in XML tags or Markdown code fences instead of pasting it directly below your instructions?
Delimiters mark the boundaries between your instructions and the material the model should process. This can make the prompt easier to interpret, but it doesn’t prevent prompt injection by itself. Treat embedded text as untrusted input and use additional safeguards where needed.
When should you add few-shot examples instead of writing more detailed rules?
Use examples when they show formatting, tone, or edge-case decisions more clearly than a general rule. Examples work best alongside clear instructions; they don’t replace rules or guarantee the model will handle every new case.