Table of Contents

Class AnthropicService

Namespace
Mythosia.AI.Services.Anthropic
Assembly
Mythosia.AI.dll
public class AnthropicService : AIService, IAIService, IFunctionRegisterable
Inheritance
AnthropicService
Implements
Inherited Members
Extension Methods

Constructors

AnthropicService(string, HttpClient)

public AnthropicService(string apiKey, HttpClient httpClient)

Parameters

apiKey string
httpClient HttpClient

AnthropicService(string, string, HttpClient)

Creates a AnthropicService with a specific model.

public AnthropicService(string apiKey, string model, HttpClient httpClient)

Parameters

apiKey string
model string
httpClient HttpClient

Properties

LastThinkingContent

Contains the thinking/reasoning content from the last non-streaming API call. Only populated when ThinkingBudget is enabled (>= 1024).

public string? LastThinkingContent { get; }

Property Value

string

Provider

The AI provider for this service

public override string Provider { get; }

Property Value

string

SupportsExtendedThinking

Returns true if the current model supports extended thinking. Supported: Claude Sonnet 4+, Claude Opus 4+, Claude Haiku 4.5+

public bool SupportsExtendedThinking { get; }

Property Value

bool

ThinkingBudget

Controls the thinking token budget for Claude extended thinking. -1: Disabled (default) - no extended thinking 1024+: Specific token budget (must be less than MaxTokens) Supported models: Claude Sonnet 4+, Claude Opus 4+, Claude Haiku 4.5+ Note: When thinking is enabled, temperature is automatically set to 1 (Claude requirement).

public int ThinkingBudget { get; set; }

Property Value

int

Methods

ApplyProviderSpecificRequestProfile(AIRequestProfile)

protected override Action ApplyProviderSpecificRequestProfile(AIRequestProfile profile)

Parameters

profile AIRequestProfile

Returns

Action

CreateFunctionMessageRequest()

Creates HTTP request with function definitions

protected override HttpRequestMessage CreateFunctionMessageRequest()

Returns

HttpRequestMessage

CreateMessageRequest()

Creates the HTTP request message for the AI service

protected override HttpRequestMessage CreateMessageRequest()

Returns

HttpRequestMessage

CreateMessageWithImageUrl(string, string)

Downloads an image from URL and converts to base64 for Claude

public Task<Message> CreateMessageWithImageUrl(string prompt, string imageUrl)

Parameters

prompt string
imageUrl string

Returns

Task<Message>

ExtractFunctionCall(string)

Extracts function call from API response

protected override (string content, FunctionCall functionCall) ExtractFunctionCall(string response)

Parameters

response string

Returns

(string content, FunctionCall functionCall)

ExtractResponseContent(string)

Extracts the response content from the API response

protected override string ExtractResponseContent(string responseContent)

Parameters

responseContent string

Returns

string

GenerateImageAsync(string, string)

Claude doesn't support image generation

public override Task<byte[]> GenerateImageAsync(string prompt, string size = "1024x1024")

Parameters

prompt string
size string

Returns

Task<byte[]>

GenerateImageUrlAsync(string, string)

Claude doesn't support image generation

public override Task<string> GenerateImageUrlAsync(string prompt, string size = "1024x1024")

Parameters

prompt string
size string

Returns

Task<string>

GetCompletionAsync(Message)

public override Task<string> GetCompletionAsync(Message message)

Parameters

message Message

Returns

Task<string>

GetCompletionWithImageAsync(string, string)

public override Task<string> GetCompletionWithImageAsync(string prompt, string imagePath)

Parameters

prompt string
imagePath string

Returns

Task<string>

GetInputTokenCountAsync()

Gets the token count for the current conversation

public override Task<uint> GetInputTokenCountAsync()

Returns

Task<uint>

GetInputTokenCountAsync(string)

Gets the token count for a specific prompt

public override Task<uint> GetInputTokenCountAsync(string prompt)

Parameters

prompt string

Returns

Task<uint>

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

uint

StreamCompletionAsync(Message, Func<string, Task>)

public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)

Parameters

message Message
messageReceivedAsync Func<string, Task>

Returns

Task

StreamParseJson(string)

Parses streaming JSON data

protected override string StreamParseJson(string jsonData)

Parameters

jsonData string

Returns

string

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

options StreamOptions
useFunctions bool
policy FunctionCallingPolicy
cancellationToken CancellationToken

Returns

IAsyncEnumerable<StreamingContent>

WithClaudeParameters(int?)

Sets Claude-specific parameters

public AnthropicService WithClaudeParameters(int? topK = null)

Parameters

topK int?

Returns

AnthropicService

WithConstitutionalAI(bool)

Enables or disables Claude's constitutional AI features

public AnthropicService WithConstitutionalAI(bool enabled = true)

Parameters

enabled bool

Returns

AnthropicService

WithTemperaturePreset(TemperaturePreset)

Sets temperature for different use cases

public AnthropicService WithTemperaturePreset(TemperaturePreset preset)

Parameters

preset TemperaturePreset

Returns

AnthropicService

WithThinkingParameters(int)

Sets Claude extended thinking parameters. Budget must be >= 1024 and less than MaxTokens. When thinking is enabled, temperature is automatically forced to 1.

public AnthropicService WithThinkingParameters(int budgetTokens)

Parameters

budgetTokens int

Returns

AnthropicService