Search⌘ K
AI Features

Messages API and Tool Use

Explore how to use Anthropic's Messages API to create and debug Claude-based applications. Understand message construction, tool integration, handling stop reasons, and running tool use loops to build efficient, explainable agent workflows.

Every Claude product an FDE builds starts from the same place. Chat assistants, document reviewers, and multi-step agents all send requests to one endpoint, the Messages API, and read back responses in one shape. Higher-level tools such as the Agent SDK sit on top of this layer.

Fluency at this layer pays off in two ways. It lets us build a working integration in a customer’s environment within hours, and it lets us explain exactly what an agent did when something goes wrong. Interviewers who ask us to build or debug a Claude workflow expect that fluency.

The examples here use the Anthropic Python SDK. Each one passes our Anthropic API key to the client through the api_key parameter.

Anatomy of a Messages API call

The Messages API is Anthropic’s interface for sending a conversation to Claude and receiving Claude’s next turn. A request names a model, sets an output limit, and passes the conversation as a list of messages. An optional system prompt sets Claude’s role and rules for the whole conversation.

The code below sends one question to Claude as a support assistant for a home goods store.

Python
import anthropic
# Pass the Anthropic API key to the client.
client = anthropic.Anthropic(api_key="{{ANTHROPIC_API_KEY}}")
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=512,
system="You are a concise support assistant for an online home goods store.",
messages=[
{"role": "user", "content": "In two sentences, how should I care for a cast iron pan?"}
],
)
# The reply arrives as a list of typed content blocks.
for block in response.content:
if block.type == "text":
print(block.text)
print("stop_reason:", response.stop_reason)
print("input tokens:", response.usage.input_tokens)
print("output tokens:", response.usage.output_tokens)
  • Line 4: anthropic.Anthropic() creates the client, and api_key passes our Anthropic API key so every request is authenticated.

  • Lines 6–13: client.messages.create sends the request. model picks the Claude model, max_tokens caps the length of the reply, system sets the assistant’s role, and messages holds the conversation.

  • Lines 16–18: The loop reads response.content, a list of content blocks, and prints only blocks whose type is text.

  • Lines 20–22: stop_reason explains why Claude stopped, and usage reports the input and ...