Working with LLM Reasoning / Thinking

Motivation

Sometimes you need to validate an LLM’s reasoning process in addition to obtaining a structured result.

Consider this scenario: a user wants to plan a vacation and specifies that their preferred destinations are Greece and Italy, with available travel dates in June, August, or September. They ask the LLM to find flight options with affordable tickets for a one-week stay. The LLM returns a structured Flight object containing departure and return dates, destinations, and prices. Even if the output adheres to the expected schema, you may want to verify that:

  • The flight dates fall within the requested months
  • The destinations are actually in Greece or Italy rather than somewhere else

If the flight details fall outside the user’s criteria, access to the LLM’s reasoning process helps you understand why it made those choices.

An even more important use case arises when the LLM cannot fulfill a request—for example, when it cannot create the requested object because the user’s criteria are ambiguous or contradictory. In this case, the thinking blocks explain what went wrong, even though no result was produced.

Concepts

  • ThinkingBlock — An abstraction that carries details about LLM reasoning, including the tag type, tag value, and reasoning text content.
  • ThinkingTagType — An enum defining the types of reasoning markers: TAG (XML-style tags like <think>), PREFIX (line prefixes like //THINKING:), and NO_PREFIX (untagged reasoning text before JSON output).
  • ThinkingOrigin — An enum distinguishing how a thinking block was produced: PROMPT_EXTRACTED (Embabel parsed reasoning text from the model’s prompt response) or NATIVE (the provider surfaced reasoning via its API, e.g. Anthropic adaptive thinking blocks).
  • ThinkingResponse<T> — A response wrapper that holds both the result object and a list of ThinkingBlock instances.
  • ThinkingException — An exception that preserves thinking blocks when object instantiation fails, enabling debugging even in error scenarios.
  • thinking() — The core PromptRunner API method that enables thinking extraction.

Example: Handling Objects and Thinking Blocks

// Configure the PromptRunner with an LLM and optional tools
PromptRunner runner = ai.withDefaultLlm()  // Example uses claude-sonnet-4-5
                        .withToolObject(Tooling.class);

String prompt = """
    What is the hottest month in Florida and its average high temperature?
    Please provide a detailed analysis of your reasoning.
    """;

// Use thinking() to enable thinking block extraction
ThinkingResponse<MonthItem> response = runner
        .thinking()
        .createObject(prompt, MonthItem.class);

// Access the structured result
MonthItem result = response.getResult();

// Access the LLM's reasoning process
List<ThinkingBlock> thinkingBlocks = response.getThinkingBlocks();

// Inspect individual thinking blocks
for (ThinkingBlock block : thinkingBlocks) {
    System.out.println("Type: " + block.getTagType());   // TAG, PREFIX, or NO_PREFIX
    System.out.println("Tag: " + block.getTagValue());   // e.g., "think", "analysis"
    System.out.println("Content: " + block.getContent());
}

Example: Handling Failures Gracefully

Use createObjectIfPossible when the LLM might not be able to produce a valid result:

ThinkingResponse<MonthItem> response = runner
        .thinking()
        .createObjectIfPossible(prompt, MonthItem.class);

MonthItem result = response.getResult();
if (result != null) {
    // Process the result
} else {
    // Object creation failed—examine the reasoning to understand why
    for (ThinkingBlock block : response.getThinkingBlocks()) {
        logger.info("LLM reasoning: {}", block.getContent());
    }
}

Thinking Tag Control

Without changing your prompt, you can control which XML-tagged reasoning blocks the model produces and returns.

Use includedTags to keep only blocks wrapped in the named tags — the model is automatically instructed (via a system prompt hint) to use those tags for its reasoning. Use excludedTags to drop blocks with specific tag names while keeping all others.

// Only return the "analysis" reasoning block; model is guided to use that tag
ThinkingResponse<MonthItem> response = runner
        .thinking(Thinking.withIncludedTags("analysis"))
        .createObject(prompt, MonthItem.class);

// Drop "scratchpad" blocks, keep all others
ThinkingResponse<MonthItem> response2 = runner
        .thinking(Thinking.withExcludedTags("scratchpad"))
        .createObject(prompt, MonthItem.class);

Native Thinking (Anthropic Adaptive Models)

Claude 5.x models (claude-sonnet-5-5, claude-opus-5-5, claude-haiku-5-5, claude-fable-5-1) and Claude ≥4.7 models (claude-opus-4-8) use Anthropic’s adaptive thinking API. When a token budget is supplied, the model decides internally when and how much to think; the reasoning is returned as a summarized block alongside the answer. Embabel marks these blocks with ThinkingOrigin.NATIVE.

To enable native thinking, pass a token budget via Thinking.withTokenBudget(n):

PromptRunner runner = ai.withLlm(
        LlmOptions.withModel("claude-sonnet-5-5")
                  .withThinking(Thinking.withTokenBudget(16000)));

ThinkingResponse<MonthItem> response = runner
        .thinking()
        .createObject(prompt, MonthItem.class);

response.getThinkingBlocks().stream()
        .filter(b -> b.getOrigin() == ThinkingOrigin.NATIVE)
        .forEach(b -> logger.info("Native reasoning: {}", b.getContent()));

The full chain of thought is encrypted in an opaque signature field and is not accessible to callers.

native thinking or prompt-extracted reasoning — not both. Including XML reasoning tags in your prompt while a thinking budget is active causes Anthropic’s API to return a reasoning_extraction refusal (ToS enforcement). Use native thinking (no tags in prompt) or prompt-extracted tags (no budget). Both together is not supported.

Provider Notes

Embabel exposes thinking through a provider-neutral API (PromptRunner.thinking() / LlmOptions.thinking). Provider integrations translate Thinking to match provider-specific capabilities; no provider-specific configuration is required at the application level.

Anthropic

Native adaptive thinking is fully supported for Claude ≥4.7 models (see [](/Native Thinking) above).

On adaptive models the token budget passed to Thinking.withTokenBudget(n) is mapped to an effort level rather than used as a hard cap:

Budget (tokens)Effort level
< 1,024LOW
< 4,096MEDIUM
< 16,384HIGH
< 32,768XHIGH
≥ 32,768MAX

in a multi-step tool loop. Use a budget of at least 16,384 (XHIGH) when reasoning across all iterations of a tool loop is important.

Known limitations:

  • Multi-turn (respond() API): When a prior assistant turn contained thinking blocks (any model with an active thinking budget), re-sending that turn via respond() drops the thinking content and signatures. Anthropic’s API requires thinking blocks — including their encrypted signatures — to be passed back intact for extended thinking sessions. Avoid combining any active thinking budget with respond() multi-turn flows until ProviderOpaqueMessage support is added.
  • Streaming: Native thinking SSE events (content_block_start(type=thinking)) are not yet extracted from the streaming path. Thinking blocks are only available from blocking (createObject) calls.

OpenAI

Reasoning tokens (reasoningContent) from o-series models (o1, o3, o4-mini, etc.) are not yet extracted. Thinking blocks will be empty even when the model reasons internally. Support is planned as a follow-up.

Ollama

All Ollama LLM services registered by Embabel’s auto-configuration set thinkingSupported = true on SpringAiLlmService, so thinking requests are sent correctly. However, the thinking field in the response is not yet extracted. Thinking blocks will be empty despite the model reasoning internally. Support is planned as a follow-up.

Google GenAI

Thinking extraction is partially supported via Spring AI (includeThoughts, thinkingBudget). Behavior may vary by model and Spring AI integration version.

Was this page helpful?

Share