daBongo LMS AI Training Courses

Claude API in Practice – A Complete Developer Reference

Lesson 1: The Messages API – Complete Request Structure

Lesson Objectives

By the end of this lesson, students should be able to:

  • Construct a complete Messages API request with all relevant parameters
  • Explain the role and appropriate values for each parameter
  • Distinguish between parameters that affect output quality vs. cost vs. behavior
  • Read and interpret all fields of a Messages API response

Lesson Content

The complete request structure.

A Messages API request (verify current parameter reference at docs.anthropic.com/en/api/messages):

json { "model": "claude-sonnet-4-6", "max_tokens": 1024, "system": "You are a helpful assistant for a software team.", "messages": [ {"role": "user", "content": "Explain this function."}, {"role": "assistant", "content": "This function..."}, {"role": "user", "content": "What about the edge case?"} ], "temperature": 0.0, "stop_sequences": ["###END###"], "metadata": {"user_id": "u_12345"} }

Parameter reference.

model – Required. The Claude model ID. Always source from docs.anthropic.com/en/docs/about-claude/models. Do not hardcode from course examples.

max_tokens – Required in practice (technically optional in some SDK versions). Caps response length. Always set in production to prevent runaway token consumption.

system – Optional but almost always used in applications. The standing instruction context. Covered in depth in the next lesson.

messages – Required. Array of {role, content} objects. Alternates user/assistant turns. The full conversation history must be included – the API is stateless.

temperature – Controls output randomness. Range 0.0-1.0. For deterministic tasks (classification, extraction, structured output): 0.0. For creative tasks (writing, brainstorming): 0.7-1.0. Default is model-specific – set explicitly for production.

stop_sequences – Array of strings that, if generated, cause Claude to stop. Use for controlled output delimiters in structured outputs. Optional.

metadata.user_id – Associates requests with a user ID for Anthropic's safety systems. Not used by your application directly – passed to Anthropic. Recommended for user-facing applications.

The response structure.

json { "id": "msg_01XFDUDYJgAACzvnptvVoYEL", "type": "message", "role": "assistant", "content": [{"type": "text", "text": "The function..."}], "model": "claude-sonnet-4-6-20251001", "stop_reason": "end_turn", "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 305} }

stop_reason: end_turn (natural completion), max_tokens (hit limit), stop_sequence (hit a stop string), tool_use (requesting a tool call). If you see max_tokens frequently, your max_tokens ceiling is too low.

usage: Log this on every production request. Input + output tokens × price = request cost.

Practical Example

A developer builds a content moderation API.

She sets temperature to 0.0 (classification needs determinism), max_tokens to 50 (moderation decisions are short), and a stop_sequence of n to enforce single-line outputs.

She logs usage on every request.

After one week of production traffic, she analyzes the usage logs and finds a request category where input tokens are 10× the average – users submitting very long documents for moderation.

She adds an input length cap to the application layer to prevent these requests from inflating cost.

Safety Notes

The metadata.user_id field passes a user identifier to Anthropic's safety systems. For user-facing applications, populating this field enables Anthropic to identify and respond to abuse patterns. This is not optional safety hygiene for consumer applications. Review Anthropic's usage policies for user-facing applications at anthropic.com/usage-policy.

Log in and enroll to access lesson quizzes.

Scroll to Top