Anthropic | Extended Thinking

Extended thinking gives Claude the ability to think through complex problems step-by-step before providing a response. When enabled, Claude creates internal reasoning (thinking blocks) that improve the quality of responses for math, coding, analysis, and other complex tasks.

Simple Example

Send a message with extended thinking enabled using the convenience method. The temperature is automatically set to 1.0 (required by the API when thinking is enabled).


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
WriteLn(Anthropic._CreateMessageWithThinking('claude-sonnet-4-20250514',
  'How many r''s are in the word strawberry?', 10000));

Advanced Example

Use the typed request/response classes for full control. Set ThinkingType to 'enabled' and ThinkingBudgetTokens to the desired token budget (minimum 1024). The response will contain thinking and text content blocks.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';

oRequest := TsgcAnthropicClass_Request_Messages.Create;
Try
  oRequest.Model := 'claude-sonnet-4-20250514';
  oRequest.MaxTokens := 16384;
  oRequest.ThinkingType := 'enabled';
  oRequest.ThinkingBudgetTokens := 10000;

  oMessage := TsgcAnthropicClass_Request_Message.Create;
  oMessage.Role := 'user';
  oMessage.Content := 'Explain the proof that there are infinitely many primes.';
  oMessages := oRequest.Messages;
  SetLength(oMessages, 1);
  oMessages[0] := oMessage;
  oRequest.Messages := oMessages;

  oResponse := Anthropic.CreateMessage(oRequest);
  Try
    for i := 0 to Length(oResponse.Content) - 1 do
    begin
      if oResponse.Content[i].ContentType = 'thinking' then
        WriteLn('Thinking: ' + oResponse.Content[i].Thinking)
      else if oResponse.Content[i].ContentType = 'text' then
        WriteLn('Response: ' + oResponse.Content[i].Text);
    end;
  Finally
    oResponse.Free;
  End;
Finally
  oMessage.Free;
  oRequest.Free;
End;

Multi-turn with Thinking

When using extended thinking in multi-turn conversations, thinking and redacted_thinking blocks from previous responses must be passed back in the conversation. Use the ContentBlocks array to include these blocks.


// Pass back thinking blocks from a previous response
oBlock := TsgcAnthropicClass_Request_Content_Block.Create;
oBlock.ContentType := 'thinking';
oBlock.Text := oPrevThinkingBlock.Thinking;   // thinking text
oBlock.Signature := oPrevThinkingBlock.Signature; // signature string

// Pass back redacted thinking blocks
oBlock := TsgcAnthropicClass_Request_Content_Block.Create;
oBlock.ContentType := 'redacted_thinking';
oBlock.Data := oPrevRedactedBlock.Data;

Properties

Notes