Skip to main content
The Provider trait defines the interface for all LLM providers in ZeroClaw. Implement this trait to integrate new language model APIs into the framework.

Trait Definition

Required Methods

chat_with_system

One-shot chat with optional system prompt. This is the only required method - all other methods have default implementations.
Option<&str>
Optional system prompt to guide the model’s behavior
&str
required
The user message to send to the model
&str
required
Model identifier (e.g., “claude-3-5-sonnet-20241022”, “gpt-4”)
f64
required
Sampling temperature (typically 0.0-1.0)
anyhow::Result<String>
The model’s response text, or an error

Optional Methods with Defaults

capabilities

Declare what features this provider supports.
ProviderCapabilities
Default: Returns all capabilities set to false.

convert_tools

Convert unified tool specifications to provider-native format.
&[ToolSpec]
required
Array of tool specifications in unified format
ToolsPayload
Provider-specific tool payload format:
  • ToolsPayload::Gemini - Gemini functionDeclarations format
  • ToolsPayload::Anthropic - Anthropic tools format
  • ToolsPayload::OpenAI - OpenAI tools format
  • ToolsPayload::PromptGuided - Text-based fallback (injected into system prompt)
Default: Returns ToolsPayload::PromptGuided with formatted instructions.

simple_chat

Simple one-shot chat without system prompt.
&str
required
User message
&str
required
Model identifier
f64
required
Sampling temperature
Default: Delegates to chat_with_system with None as system prompt.

chat_with_history

Multi-turn conversation with message history.
&[ChatMessage]
required
Array of chat messages with roles (system, user, assistant, tool)
&str
required
Model identifier
f64
required
Sampling temperature
Default: Extracts system message and last user message, delegates to chat_with_system.

chat

Structured chat API for agent loop callers. Handles tool injection.
ChatRequest<'_>
required
&str
required
Model identifier
f64
required
Sampling temperature
ChatResponse
Default: If tools are provided but provider doesn’t support native tools, injects tool instructions into system prompt.

chat_with_tools

Chat with native tool calling support.
&[ChatMessage]
required
Message history
&[serde_json::Value]
required
Provider-native tool definitions
&str
required
Model identifier
f64
required
Sampling temperature
Default: Delegates to chat_with_history and returns empty tool_calls vector.

supports_native_tools

Check if provider supports native function calling API. Default: Returns capabilities().native_tool_calling.

supports_vision

Check if provider supports multimodal vision input. Default: Returns capabilities().vision.

supports_streaming

Check if provider supports streaming responses. Default: Returns false.

stream_chat_with_system

Streaming version of chat_with_system.
StreamOptions
Default: Returns empty stream (not supported).

stream_chat_with_history

Streaming version of chat_with_history. Default: Extracts last message and delegates to stream_chat_with_system.

warmup

Warm up HTTP connection pool (TLS handshake, DNS, HTTP/2). Default: No-op.

Types

ChatMessage

Helper constructors:
  • ChatMessage::system(content) - Create system message
  • ChatMessage::user(content) - Create user message
  • ChatMessage::assistant(content) - Create assistant message
  • ChatMessage::tool(content) - Create tool result message

ToolCall

TokenUsage

NormalizedStopReason

Provider-specific mappings:
  • NormalizedStopReason::from_openai_finish_reason(raw: &str)
  • NormalizedStopReason::from_anthropic_stop_reason(raw: &str)
  • NormalizedStopReason::from_bedrock_stop_reason(raw: &str)
  • NormalizedStopReason::from_gemini_finish_reason(raw: &str)

Implementation Example

Factory Registration

After implementing the trait, register your provider in the factory:

Best Practices

Security: Never log API keys, tokens, or sensitive request/response data. Use the security policy framework for credential handling.
Error Handling: Use explicit errors for unsupported features rather than silent fallbacks. Return ProviderCapabilityError when a capability is not available.
Compatibility: Keep factory registration keys stable (e.g., "openai", "anthropic"). Changes require user migration.