Skip to main content

Three providers, one interface

Every provider implements IChatProvider:
AI/Contracts/IChatProvider.cs
ChatProviderFactory.GetProvider(provider_name) maps the model’s provider to a service. Unknown or empty names get Groq. Store only the canonical names groq, google and cloudflare in ai_models.provider_name: ModelSelector ignores any other value, even though the factory also accepts the aliases google-ai-studio and workers-ai. All three send an OpenAI-style body (model, messages, max_tokens default 4096, temperature default 0.7) and read choices[0].message.content and usage.

Cloudflare AI Gateway

Groq and Google chat require the gateway: CloudflareAiGatewaySettings.EnsureGatewayConfiguredForChat throws if CLOUDFLARE_AI_GATEWAY_ACCOUNT_ID or CLOUDFLARE_AI_GATEWAY_ID is missing.
With CLOUDFLARE_AI_GATEWAY_USE_BYOK=true the provider keys are stored in the gateway (). The API sends only the gateway token (CLOUDFLARE_AI_GATEWAY_TOKEN, as cf-aig-authorization), and the key pool below isn’t used for chat. Production runs in this mode (wrangler.jsonc sets the variable).

The key pool

ProviderConfigService caches the providers and provider_api_keys tables in memory. Admin changes to providers and keys refresh the cache immediately. GetNextKeyAsync(provider) returns the least recently used key that is:
  • on an enabled provider,
  • is_active,
  • not in cooldown (cooldown_until is empty or past),
  • not already tried for this request.
After each call the service reports back. Success resets failure_count and cooldown_until and bumps total_calls. Failure is classified and puts the key in cooldown: When no key is left the service throws ProviderExhaustedException, and the controller falls back. Admins can watch and reset this from Catalog → Providers & keys in the admin panel (key routes).

Fallback

ArenaController.ExecuteWithFallbackAsync wraps every non-streaming call: The response keeps the original model name in both cases, so the battle and its comparison stay attributed to the model that was picked. Search comparisons for [System Note] to find fallback answers.

Adding a provider

1

Implement IChatProvider

Add a service under AI/Providers and register it with AddHttpClient<T> (45 s timeout) in Program.cs.
2

Route it

Add the name to ChatProviderFactory.GetProvider and to ModelSelector.SupportedProviders (and to ManualOnlyProviders if it shouldn’t join random battles).
3

Configure and seed

Add the credentials as Worker secrets (names only in code, values in Cloudflare), a providers row and ai_models rows. Seed model_leaderboard for the new models so they appear on the leaderboard.
4

Test

Extend ModelSelectorTests and add provider tests in tests/DualMind.API.Tests.
Last modified on October 18, 2026