Integrating LLM APIs · LLM API Foundations

System Prompts, Messages & Prompt Templates

Structuring instructions, context and user input reliably.

Treat prompts as code: version them, review them, test them. Put stable role, rules and output expectations in the system prompt; put task data in user messages, clearly delimited (e.g. XML-style tags) so the model can tell instructions from data. Give the model context and reasons rather than all-caps rules, include examples for tricky formats, and keep untrusted input (user text, retrieved documents, tool output) separate from your instructions. Store templates in the repo with IDs and versions you log on every call.

Anatomy of a prompt

Part Contents Changes
System prompt Role, policies, tone, output contract Rarely (versioned)
Few-shot examples Input → ideal output pairs Occasionally
Context Retrieved docs, user profile, tool results Per request
User message The actual request Per request
Output format Schema / structured outputs With API versions

Python (versioned template)

PROMPT_ID = "ticket-summary@v7"
SYSTEM = (
    "You summarize customer support tickets for agents. Agents read summaries on small screens, "
    "so keep them under 80 words. Never invent order numbers; if one is missing, say so."
)

def build_messages(ticket: Ticket) -> list[dict]:
    return [{"role": "user", "content":
             f"<ticket id=\"{ticket.id}\">\n{ticket.body}\n</ticket>\n\nSummarize the ticket above."}]

resp = client.messages.create(model=MODEL, max_tokens=1024, system=SYSTEM, messages=build_messages(t))
log.info("llm_call", prompt_id=PROMPT_ID, input_tokens=resp.usage.input_tokens)

Common pitfalls

  • Concatenating user input into the instructions.
  • Prompts edited in production without versioning or tests.
  • Over-constraining with shouting rules instead of explaining intent.

Key points

  • Prompts are versioned code
  • Delimit untrusted data
  • Explain intent; show examples

Further reading: Prompt engineering overview

Practice questions

Why delimit user-provided text with tags in prompts?

Answer

It makes clear which text is data versus instructions, improving reliability and reducing (not eliminating) prompt-injection risk.

Why log a prompt template ID/version with each call?

Answer

To correlate quality changes, regressions and costs with specific prompt versions and to reproduce issues.

Open in the app to mark lessons complete, take notes and track your progress.