Claude API Prompt Template Library: Examples That Work
Developers searching for a "Claude API prompt template library" usually want two things: a set of proven prompt structures they can copy into their own code, and a way to manage those templates so they don't get scattered across config files and Slack messages. This article gives you both — a working library of reusable prompt templates for the most common Claude API tasks, plus patterns for storing and versioning them in a real application.
Every template below follows the same structure: a system prompt that sets role and constraints, a user message template with placeholders, and notes on where this pattern tends to break. You can drop these directly into requests against the Claude API or against a compatible gateway like SubToAPI (https://subtoapi.app), which exposes the same message format over a standard HTTPS endpoint.
Why a Template Library Matters
Ad-hoc prompting works fine for prototypes. Once you have multiple features calling Claude — support replies, data extraction, content moderation — you need consistency. A template library solves three problems:
- Reproducibility: the same input produces structurally similar output every time.
- Maintainability: you update one template instead of hunting through five files.
- Testability: you can snapshot-test prompts against expected output shapes.
Store templates as plain text or JSON with named placeholders ({{document}}, {{tone}}, {{schema}}) and interpolate at request time. Avoid building elaborate templating engines — simple string replacement is usually enough.
Core Template Structure
{
"name": "extract-invoice-fields",
"system": "You extract structured data from invoices. Always return valid JSON matching the given schema. If a field is missing, use null.",
"user_template": "Schema:\n{{schema}}\n\nInvoice text:\n{{invoice_text}}",
"max_tokens": 500,
"temperature": 0
}
Keep temperature at 0 for extraction and classification tasks — you want determinism, not creativity. Reserve higher temperatures for drafting and brainstorming templates.
Example Templates
1. Summarization
System: You summarize technical documents in 3-5 bullet points.
No preamble, no "here is a summary" — output bullets only.
User: Summarize the following:
{{document}}
2. Structured Data Extraction
System: You are a data extraction engine. Return only valid JSON.
Never include explanations or markdown formatting around the JSON.
User: Extract the following fields from this text: {{fields}}
Text:
{{source_text}}
3. Classification
System: Classify the input into exactly one of these categories: {{categories}}
Respond with only the category name, nothing else.
User: {{input_text}}
4. Tone-Controlled Rewriting
System: Rewrite text to match a {{tone}} tone while preserving all factual content. Do not add or remove information.
User: {{original_text}}
5. Multi-Turn Support Agent
System: You are a support agent for {{product_name}}. Answer only using the provided knowledge base excerpts. If the answer isn't in the excerpts, say you don't know and offer to escalate.
Knowledge base:
{{kb_excerpts}}
User: {{customer_message}}
6. Code Review Comment Generator
System: You review pull request diffs. Point out bugs, security issues, and style violations. Skip praise — only output actionable comments, one per line.
User: Diff:
{{diff}}
Calling Templates Through the API
Once a template is filled in, the request itself is standard. Here's an example using the extraction template against SubToAPI's endpoint, which maps directly onto Claude's message format:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 500,
"system": "You are a data extraction engine. Return only valid JSON.",
"messages": [
{"role": "user", "content": "Extract name, amount, due_date from: Invoice #442, Total: $1,230.00, Due: 2024-09-15"}
]
}'
In JavaScript, wrapping templates as functions keeps the interpolation logic contained:
function buildExtractionPrompt(schema, text) {
return {
system: "You are a data extraction engine. Return only valid JSON matching the schema. Use null for missing fields.",
messages: [
{ role: "user", content: `Schema:\n${schema}\n\nText:\n${text}` }
]
};
}
const prompt = buildExtractionPrompt(
'{"name": "string", "amount": "number", "due_date": "string"}',
invoiceText
);
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"content-type": "application/json"
},
body: JSON.stringify({ model: "claude-sonnet-4", max_tokens: 500, ...prompt })
});
If your templates need to trigger specific actions rather than just return text, pair them with tool definitions — see /docs/tools for how tool use integrates with the message format. For the full request/response shape used in every example here, check /docs/messages.
Organizing Templates in Your Codebase
Three practical patterns, from simplest to most structured:
- Flat JSON file — a single
prompts.jsonwith name/system/user_template keys. Fine for small projects. - Directory of
.txtfiles — one file per template, loaded at startup. Easier to diff in git and review in PRs. - Database-backed templates — needed once non-engineers (support, marketing) need to edit prompts without a deploy. Add a version column so you can roll back a bad edit.
Whichever you choose, write a small test suite that sends each template with a fixed sample input and asserts on output shape (valid JSON, correct category from a fixed list, non-empty bullets). This catches regressions when you tweak wording.
Versioning and Rollback
Treat prompt changes like schema migrations. Tag each template version, log which version produced each response (store it alongside the request in your database), and keep the last two or three versions available so you can roll back if a wording change degrades output quality. This matters more than it sounds — a single adjective change in a system prompt can shift classification accuracy noticeably.
Getting Started
If you're building this library against your own Claude access, SubToAPI turns that access into a standard HTTPS API with application keys, streaming, and usage tracking per key — useful once you have multiple templates running in production and want to see which ones consume the most tokens. Start with /docs/quickstart, review the full message schema at /docs/messages, and check /docs/streaming if any of your templates need token-by-token output. Sign up at /signup or compare plans at /pricing.
Questions
Do I need a different API key per template? No — templates are just message content, not authentication boundaries. Use separate keys per application or environment instead, and track which template version produced each request in your own logs.
Should system prompts or user messages hold the template variables? Keep static instructions (role, output format, constraints) in the system prompt and put variable content (the actual document, text, or data) in the user message. This keeps templates easier to version and test.
How do I prevent prompt injection in templates that embed user-supplied text? Explicitly instruct the model to treat embedded text as data, not instructions, and wrap untrusted input in clear delimiters (like triple backticks or XML-style tags) so the model can distinguish your instructions from the user's content.