Skip to main content

Provider System

The Provider system abstracts model inference backends behind a uniform interface, enabling ZeroClaw to work with any LLM provider (OpenAI, Anthropic, local models, etc.) through a single consistent API.

Architecture Overview

Provider Trait

All providers implement the Provider trait from src/providers/traits.rs:

Provider Capabilities

Providers declare capabilities to enable intelligent adaptation:

Native Tool Calling

Providers that support native tool calling return structured tool calls:
Native Tool Calling Providers:
  • OpenAI (GPT-4, GPT-3.5)
  • Anthropic (Claude)
  • Gemini
  • Azure OpenAI

Prompt-Guided Tool Calling

Providers without native tool support use prompt injection:
Prompt-Guided Example:

OpenAI Provider Implementation

Real implementation from src/providers/openai.rs:

Provider Factory

Providers are instantiated via factory function in src/providers/mod.rs:

Reliable Provider Wrapper

The ReliableProvider wrapper adds fallback chains and retry logic:

Stop Reason Normalization

Providers normalize stop reasons to a common enum:

Token Usage Tracking

Configuration

Providers are configured in zeroclaw.toml:

Built-in Providers

Adding a New Provider

From AGENTS.md §7.1:
  1. Create provider file: src/providers/new_provider.rs
  1. Register in factory: src/providers/mod.rs
  1. Add tests: Test factory wiring and error paths
  1. Update docs: Add to docs/providers-reference.md

Best Practices

Error Handling

  • Return structured errors with context
  • Distinguish retryable vs. non-retryable errors
  • Log API errors at debug level, not error

Rate Limiting

  • Respect provider rate limits
  • Implement exponential backoff
  • Surface rate limit errors to user

Security

  • Never log API keys or tokens
  • Use environment variables for credentials
  • Validate API responses before parsing

Performance

  • Reuse HTTP clients (connection pooling)
  • Implement warmup() for connection pre-warming
  • Stream responses when possible

Next Steps

  • Channels - Channel system architecture
  • Tools - Tool system and capabilities
  • Security - Security policy and validation