Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Anthropics Anthropic sdk python Messages Create With Thinking

From Leeroopedia
Knowledge Sources
Domains Extended_Thinking, LLM, Reasoning
Last Updated 2026-02-15 00:00 GMT

Overview

The Messages.create() method is the primary entry point for making API requests to Claude. When invoked with the thinking parameter, it augments the standard message creation flow with extended thinking capabilities. This page documents Messages.create() from the perspective of thinking configuration integration.

Method Signature

Source: src/anthropic/resources/messages/messages.py (Lines 927-1003)

@required_args(["max_tokens", "messages", "model"], ["max_tokens", "messages", "model", "stream"])
def create(
    self,
    *,
    max_tokens: int,
    messages: Iterable[MessageParam],
    model: ModelParam,
    inference_geo: Optional[str] | Omit = omit,
    metadata: MetadataParam | Omit = omit,
    output_config: OutputConfigParam | Omit = omit,
    service_tier: Literal["auto", "standard_only"] | Omit = omit,
    stop_sequences: SequenceNotStr[str] | Omit = omit,
    stream: Literal[False] | Literal[True] | Omit = omit,
    system: Union[str, Iterable[TextBlockParam]] | Omit = omit,
    temperature: float | Omit = omit,
    thinking: ThinkingConfigParam | Omit = omit,
    tool_choice: ToolChoiceParam | Omit = omit,
    tools: Iterable[ToolUnionParam] | Omit = omit,
    top_k: int | Omit = omit,
    top_p: float | Omit = omit,
    extra_headers: Headers | None = None,
    extra_query: Query | None = None,
    extra_body: Body | None = None,
    timeout: float | httpx.Timeout | None | NotGiven = not_given,
) -> Message | Stream[RawMessageStreamEvent]:

Thinking-Specific Parameters

Parameter Type Description
thinking Omit Configures extended thinking. Accepts {"type": "enabled", "budget_tokens": N}, {"type": "disabled"}, or {"type": "adaptive"}. Defaults to omit (not sent).
max_tokens int Maximum tokens for the entire response (thinking + text). When using enabled thinking, budget_tokens must be less than this value.

Runtime Warning for Thinking Mode Deprecation

Source: src/anthropic/resources/messages/messages.py (Lines 965-970)

if model in MODELS_TO_WARN_WITH_THINKING_ENABLED and thinking and thinking["type"] == "enabled":
    warnings.warn(
        f"Using Claude with {model} and 'thinking.type=enabled' is deprecated. "
        f"Use 'thinking.type=adaptive' instead which results in better model performance "
        f"in our testing: https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking",
        UserWarning,
        stacklevel=3,
    )

The constant MODELS_TO_WARN_WITH_THINKING_ENABLED is defined at line 74:

MODELS_TO_WARN_WITH_THINKING_ENABLED = ["claude-opus-4-6"]

This warning fires when:

  • The model is in the MODELS_TO_WARN_WITH_THINKING_ENABLED list
  • The thinking parameter is provided (truthy)
  • The thinking["type"] is specifically "enabled"

The warning uses UserWarning (not DeprecationWarning) and stacklevel=3 to point to the caller's code.

Request Body Construction

Source: src/anthropic/resources/messages/messages.py (Lines 972-1003)

return self._post(
    "/v1/messages",
    body=maybe_transform(
        {
            "max_tokens": max_tokens,
            "messages": messages,
            "model": model,
            "inference_geo": inference_geo,
            "metadata": metadata,
            "output_config": output_config,
            "service_tier": service_tier,
            "stop_sequences": stop_sequences,
            "stream": stream,
            "system": system,
            "temperature": temperature,
            "thinking": thinking,
            "tool_choice": tool_choice,
            "tools": tools,
            "top_k": top_k,
            "top_p": top_p,
        },
        message_create_params.MessageCreateParamsStreaming
        if stream
        else message_create_params.MessageCreateParamsNonStreaming,
    ),
    options=make_request_options(
        extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
    ),
    cast_to=Message,
    stream=stream or False,
    stream_cls=Stream[RawMessageStreamEvent],
)

The thinking parameter is included in the request body dictionary. When set to omit, the maybe_transform function strips it from the serialized body.

Usage Examples

Basic Thinking Request

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=16000,
    thinking={"type": "enabled", "budget_tokens": 10000},
    messages=[{"role": "user", "content": "Solve this step by step: What is 15% of 340?"}]
)

for block in message.content:
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
    elif block.type == "text":
        print(f"Answer: {block.text}")

Adaptive Thinking Request

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=16000,
    thinking={"type": "adaptive"},
    messages=[{"role": "user", "content": "What is 2+2?"}]
)

Streaming with Thinking

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=16000,
    thinking={"type": "enabled", "budget_tokens": 10000},
    messages=[{"role": "user", "content": "Explain quantum computing"}]
) as stream:
    for event in stream:
        if event.type == "thinking":
            print(f"[thinking] {event.thinking}", end="", flush=True)
        elif event.type == "text":
            print(event.text, end="", flush=True)

Response Structure

When thinking is enabled, the response Message.content list contains blocks in this order:

  1. ThinkingBlock (zero or more): Contains thinking (reasoning text) and signature (cryptographic verification)
  2. TextBlock (one or more): Contains text (the visible answer)

The return type is Message for non-streaming calls, or Stream[RawMessageStreamEvent] for streaming calls.

Dependencies

  • httpx: HTTP client used for the underlying API call and timeout configuration.
  • warnings: Python standard library module used to emit UserWarning for deprecated thinking modes and DeprecationWarning for deprecated models.
  • anthropic.types.ThinkingConfigParam: The union type accepted by the thinking parameter.
  • anthropic._types.Omit: Sentinel type used to indicate that a parameter should be excluded from the request body.

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment