Anthropic | Extended Thinking

O extended thinking dá ao Claude a capacidade de pensar em problemas complexos passo a passo antes de fornecer uma resposta. Quando habilitado, o Claude cria raciocínio interno (thinking blocks) que melhora a qualidade das respostas para matemática, programação, análise e outras tarefas complexas.

Exemplo Simples

Envia uma mensagem com extended thinking habilitado usando o método de conveniência. A temperatura é definida automaticamente como 1.0 (exigido pela API quando o thinking está habilitado).


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));

Exemplo avançado

Use as classes tipadas de requisição/resposta para controle total. Defina ThinkingType como 'enabled' e ThinkingBudgetTokens com o orçamento de tokens desejado (mínimo 1024). A resposta conterá blocos de conteúdo thinking e text.


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 com Thinking

Ao utilizar extended thinking em conversas com múltiplas interações, os blocos thinking e redacted_thinking de respostas anteriores devem ser repassados na conversa. Utilize o array ContentBlocks para incluir esses blocos.


// 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

Notas