The Anthropic Developer Platform – From First Call to Production By the end of this lesson, students should be able to: API prompt engineering vs. chat prompt engineering. In a chat interface, you can refine prompts interactively and individually for each conversation. In an API integration, your prompt must work reliably across all the variable inputs your application will send. A prompt that works 95% of the time in testing will fail 5% of the time in production – at 10,000 daily requests, that is 500 failures per day. API-grade prompt engineering requires: System prompts for consistent behavior. The system prompt in an API integration is your standing instruction contract. It should cover: A well-written system prompt makes the application resilient to edge cases because the model has explicit instructions for handling them. Structured output formatting. For applications that parse Claude's output programmatically, specify the exact format: “ Explicit format instructions dramatically reduce parsing failures. For critical structured output, also implement output validation in your application (parse the JSON, validate the schema, handle malformed responses gracefully). Few-shot examples. For tasks with specific output patterns that are difficult to specify in instructions alone, few-shot examples in the prompt demonstrate the expected format: “` User: "My order hasn't arrived." Assistant: {"category": "shipping", "confidence": 0.97, "summary": "Customer reporting undelivered order"} User: "I was charged twice." Assistant: {"category": "billing", "confidence": 0.99, "summary": "Customer reporting duplicate charge"} “` Two to three high-quality examples in the prompt often produce better output consistency than longer instruction text. Common API prompt reliability failures. A developer builds a document classification API. His initial prompt gets 88% accuracy on test cases. He adds: (1) explicit output format instruction specifying valid categories, (2) two few-shot examples for the ambiguous edge cases, (3) explicit instruction for what to return when the document does not fit any category. He retests: accuracy improves to 96%. The three additions address the three most common failure modes: format inconsistency, edge case handling, and out-of-scope inputs. When building API integrations that process user-supplied inputs, implement input validation before passing content to the API. Malformed, excessively long, or adversarially crafted inputs can cause unexpected behavior or inflate costs. Validate and sanitize user inputs before including them in API requests. Also review Anthropic's usage policies for applications that process user-generated content. Log in and enroll to access lesson quizzes.
Lesson 4: Prompt Engineering for API Reliability
Lesson Objectives
Lesson Content
Always respond with a JSON object with the following structure: { "category": "<one of: billing, technical, shipping, other>", "confidence": <0.0 to 1.0>, "summary": "<one sentence summary of the issue>" } Respond with only the JSON object - no preamble, no explanation. “Practical Example
Safety Notes