Class AnthropicService
public class AnthropicService : AIService, IAIService, IFunctionRegisterable
- Inheritance
-
AnthropicService
- Implements
- Inherited Members
- Extension Methods
Constructors
AnthropicService(string, HttpClient)
public AnthropicService(string apiKey, HttpClient httpClient)
Parameters
apiKeystringhttpClientHttpClient
AnthropicService(string, string, HttpClient)
Creates a AnthropicService with a specific model.
public AnthropicService(string apiKey, string model, HttpClient httpClient)
Parameters
apiKeystringmodelstringhttpClientHttpClient
Properties
AdaptiveThinkingDisplay
Controls whether adaptive-thinking models return readable summarized reasoning. This value is sent only when adaptive thinking is explicitly enabled.
public ClaudeThinkingDisplay AdaptiveThinkingDisplay { get; set; }
Property Value
AdaptiveThinkingEffort
Selects the low/medium/high/xhigh/max effort range on adaptive-thinking Claude models. XHigh is rejected for Opus 4.6 and Sonnet 4.6 because those models do not support it. Auto preserves the legacy ThinkingBudget mapping using effort levels the selected model supports.
public ClaudeReasoningEffort AdaptiveThinkingEffort { get; set; }
Property Value
LastThinkingContent
Contains the thinking/reasoning content from the last non-streaming API call. Populated when the response contains a thinking block. This can occur even when ThinkingBudget is disabled for models whose adaptive thinking is always on.
public string? LastThinkingContent { get; }
Property Value
Provider
The AI provider for this service
public override string Provider { get; }
Property Value
SupportsExtendedThinking
Returns true if the current model supports a Claude thinking mode.
Despite this legacy property name, a true value can mean either manual extended
thinking (thinking.type=enabled) or adaptive thinking
(thinking.type=adaptive). Claude Fable 5, Mythos 5, Opus 5, Sonnet 5, and Opus 4.7+
are adaptive-only and do not accept budget_tokens.
public bool SupportsExtendedThinking { get; }
Property Value
ThinkingBudget
Controls the thinking token budget for Claude extended thinking. -1 requests disabled thinking where the model permits it. Fable 5 and Mythos 5 cannot disable thinking, so this setting uses their lowest effort and omits the readable summary instead. 1024+ is an exact token budget on manual-thinking models. On adaptive-thinking models, it retains the legacy high/xhigh/max effort mapping unless AdaptiveThinkingEffort is set explicitly. Supported models: Claude Fable 5, Claude Mythos 5, Claude Sonnet 5 / 4+, Claude Opus 5 / 4+, and Claude Haiku 4.5+. Note: adaptive-thinking models omit temperature; legacy manual-thinking models force it to 1.
public int ThinkingBudget { get; set; }
Property Value
Methods
ApplyProviderSpecificRequestProfile(AIRequestProfile)
protected override Action ApplyProviderSpecificRequestProfile(AIRequestProfile profile)
Parameters
profileAIRequestProfile
Returns
ApplyRequestProfile(AIRequestProfile)
protected override Action ApplyRequestProfile(AIRequestProfile profile)
Parameters
profileAIRequestProfile
Returns
CreateFunctionMessageRequest()
Creates HTTP request with function definitions
protected override HttpRequestMessage CreateFunctionMessageRequest()
Returns
CreateMessageRequest()
Creates the HTTP request message for the AI service
protected override HttpRequestMessage CreateMessageRequest()
Returns
CreateMessageWithImageUrl(string, string)
Downloads an image from URL and converts to base64 for Claude
public Task<Message> CreateMessageWithImageUrl(string prompt, string imageUrl)
Parameters
Returns
ExtractFunctionCalls(string)
Extracts every function call from one API response.
protected override (string content, FunctionCallBatch functionCalls) ExtractFunctionCalls(string response)
Parameters
responsestring
Returns
ExtractResponseContent(string)
Extracts the response content from the API response
protected override string ExtractResponseContent(string responseContent)
Parameters
responseContentstring
Returns
GetCompletionAsync(Message)
public override Task<string> GetCompletionAsync(Message message)
Parameters
messageMessage
Returns
GetCompletionWithImageAsync(string, string)
public override Task<string> GetCompletionWithImageAsync(string prompt, string imagePath)
Parameters
Returns
GetInputTokenCountAsync()
Gets the token count for the current conversation
public override Task<uint> GetInputTokenCountAsync()
Returns
GetInputTokenCountAsync(string)
Gets the token count for a specific prompt
public override Task<uint> GetInputTokenCountAsync(string prompt)
Parameters
promptstring
Returns
GetModelMaxOutputTokens()
Returns the maximum output tokens allowed for the current model. Override in each service to provide model-specific limits.
protected override uint GetModelMaxOutputTokens()
Returns
StreamCompletionAsync(Message, Func<string, Task>)
public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)
Parameters
Returns
StreamParseJson(string)
Parses streaming JSON data
protected override string StreamParseJson(string jsonData)
Parameters
jsonDatastring
Returns
StreamRoundAsync(StreamOptions, bool, FunctionCallingPolicy, CancellationToken)
Executes a single streaming round: sends an HTTP request, reads the SSE stream, yields chunks, and handles function execution if detected. Yield a FunctionResult to signal the template to continue to the next round; otherwise the stream ends.
protected override IAsyncEnumerable<StreamingContent> StreamRoundAsync(StreamOptions options, bool useFunctions, FunctionCallingPolicy policy, CancellationToken cancellationToken)
Parameters
optionsStreamOptionsuseFunctionsboolpolicyFunctionCallingPolicycancellationTokenCancellationToken
Returns
WithAdaptiveThinkingParameters(ClaudeReasoningEffort, ClaudeThinkingDisplay)
Enables adaptive thinking with an explicit Claude effort and display policy.
public AnthropicService WithAdaptiveThinkingParameters(ClaudeReasoningEffort effort, ClaudeThinkingDisplay display = ClaudeThinkingDisplay.Summarized)
Parameters
effortClaudeReasoningEffortdisplayClaudeThinkingDisplay
Returns
WithClaudeParameters(int?)
Sets Claude-specific parameters
public AnthropicService WithClaudeParameters(int? topK = null)
Parameters
topKint?
Returns
WithConstitutionalAI(bool)
Enables or disables Claude's constitutional AI features
public AnthropicService WithConstitutionalAI(bool enabled = true)
Parameters
enabledbool
Returns
WithTemperaturePreset(TemperaturePreset)
Sets temperature for different use cases
public AnthropicService WithTemperaturePreset(TemperaturePreset preset)
Parameters
presetTemperaturePreset
Returns
WithThinkingParameters(int)
Sets Claude thinking through the legacy token-budget API.
On manual-thinking models, the budget must be at least 1024 and is sent as
budget_tokens; max_tokens is adjusted when necessary and temperature is
forced to 1. On adaptive-only models, the numeric budget is retained for compatibility
and mapped to high/xhigh/max effort instead of being sent to Anthropic.
public AnthropicService WithThinkingParameters(int budgetTokens)
Parameters
budgetTokensint