OpenAiCompatibleBackend

public sealed class OpenAiCompatibleBackend : IAiBackend
IAiBackend implementation talking the OpenAI Chat Completions wire format. The system/user/assistant message shape is flat (no nested content blocks like Anthropic, no separate systemInstruction like Gemini) and tool calls round-trip through an explicit tool_call_id. Forces tool_choice = "required" so the model cannot answer in prose -- the conversation loop expects every turn to be a tool invocation. This class is provider-neutral: OpenAI and DeepSeek both speak the same schema, differing only in endpoint URL and default model. The caller supplies those at construction.

Constructors

OpenAiCompatibleBackend

#
public OpenAiCompatibleBackend(AiProvider provider, string endpoint, string defaultModel, string apiKey)
provider (AiProvider)Which provider this backend talks to. Used for the Provider property and in error messages.
endpoint (string)Full URL of the chat-completions endpoint, e.g. https://api.openai.com/v1/chat/completions.
defaultModel (string)Initial value for Model. The caller can override it later.
apiKey (string)Bearer token passed in the Authorization header.

Properties

MaxRetries

#
public int MaxRetries { get; set; }
Maximum number of automatic 429/503 retries before giving up.

MaxTokens

#
public int MaxTokens { get; set; }
Maximum tokens the model may produce in a single turn.

Model

#
public string Model { get; set; }
Model identifier passed to the API.

Provider

#
public AiProvider Provider { get; }
Provider

TranscriptCallback

#
public Action<string, string> TranscriptCallback { get; set; }
Optional hook invoked with (requestJson, responseBody) after every HTTP exchange, including non-2xx responses and retries. The host uses this to maintain a complete transcript of what was sent and received. Backends must invoke this before throwing on non-success status codes.
TranscriptCallback

Methods

Generate

#
public Task<AiBackendResponse> Generate(AiMessage[] history, AiTool[] tools, CancellationToken cancellation)
history (AiMessage[])
tools (AiTool[])
cancellation (CancellationToken)
Generate