Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nearai/ironclaw/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The LLM module provides integration with multiple language model providers:- NEAR AI (default): Session token or API key auth via Chat Completions API
- OpenAI: Direct API access with your own key
- Anthropic: Claude models via direct API
- Ollama: Local model inference
- OpenAI-compatible: Any endpoint that speaks the OpenAI API
- Tinfoil: Private inference endpoints
Core Types
LlmProvider Trait
The main trait that all LLM providers must implement.Methods
Return the model identifier
Return (input_cost, output_cost) per token in USD
Generate a completion for a conversation
complete_with_tools
async fn(&self, request: ToolCompletionRequest) -> Result<ToolCompletionResponse>
Generate a completion with tool calling support
List available models from the provider (optional)
Fetch metadata about the model (context length, etc.)
ChatMessage
A message in a conversation.Message role (System, User, Assistant, Tool)
Message content/text
Tool call ID if this is a tool result message
Tool name for tool result messages
Tool calls made by the assistant
Constructors
Create a system message
Create a user message
Create an assistant message
Create an assistant message with tool calls
tool_result
fn(tool_call_id: impl Into<String>, name: impl Into<String>, content: impl Into<String>) -> Self
Create a tool result message
Example
CompletionRequest
Request for a chat completion.Conversation history
Optional per-request model override
Maximum tokens to generate
Sampling temperature (0.0 to 2.0)
Sequences that stop generation
Opaque metadata passed to provider
Methods
Create a new completion request
Set model override
Set max tokens
Set temperature
Example
CompletionResponse
Response from a chat completion.Generated text content
Number of tokens in the prompt
Number of tokens generated
Why generation stopped
FinishReason
Model naturally completed the response
Hit max_tokens limit
Model wants to use tools
Filtered by content policy
Unknown reason
ToolCompletionRequest
Request for a completion with tool use support.Conversation history
Available tools for the model
Optional model override
Maximum tokens to generate
Sampling temperature
How to handle tools: “auto”, “required”, or “none”
Opaque metadata
Methods
Create a new tool completion request
Set tool choice mode (“auto”, “required”, “none”)
Example
ToolCompletionResponse
Response from a tool-enabled completion.Text content (may be empty if tool calls present)
Tool calls requested by the model
Prompt tokens
Generated tokens
Why generation stopped
ToolCall
A tool call requested by the LLM.Unique call identifier
Tool name
Tool arguments as JSON
ToolDefinition
Definition of a tool for the LLM.Tool name
What the tool does
JSON Schema for parameters
ToolResult
Result of tool execution to send back to the LLM.ID of the tool call this is a result for
Tool name
Result content
Whether this result is an error
Provider Creation
create_llm_provider
create_llm_provider
fn(config: &LlmConfig, session: Arc<SessionManager>) -> Result<Arc<dyn LlmProvider>>
Create an LLM provider based on configuration
build_provider_chain
build_provider_chain
fn(config: &LlmConfig, session: Arc<SessionManager>) -> Result<(Arc<dyn LlmProvider>, Option<Arc<dyn LlmProvider>>)>
Build the full provider chain with retry, failover, circuit breaker, cache, etc. Returns (main_provider, cheap_provider).
- Raw provider (from config)
- RetryProvider (exponential backoff)
- SmartRoutingProvider (cheap/primary split)
- FailoverProvider (fallback model)
- CircuitBreakerProvider (fast-fail when degraded)
- CachedProvider (response cache)
Resilience Features
RetryProvider
Retries failed requests with exponential backoff.Wrap a provider with retry logic
RetryConfig
Maximum number of retry attempts
FailoverProvider
Fails over to backup providers when primary fails.Create a failover provider with ordered list of providers
Create failover with cooldown (temporary disable after failures)
CooldownConfig
How long to disable a provider after failures
Number of failures before cooldown
CircuitBreakerProvider
Fast-fails when backend is degraded (circuit breaker pattern).Wrap a provider with circuit breaker
CircuitBreakerConfig
Failures before opening circuit
How long to wait before testing recovery
Test calls allowed in half-open state
CachedProvider
Caches responses to avoid redundant API calls.Wrap a provider with response caching
ResponseCacheConfig
Cache entry time-to-live
Maximum cache entries
SmartRoutingProvider
Routes simple requests to cheap model, complex to primary.new
fn(primary: Arc<dyn LlmProvider>, cheap: Arc<dyn LlmProvider>, config: SmartRoutingConfig) -> Self
Create smart routing between two providers
SmartRoutingConfig
Retry on primary if cheap model fails
Complexity score above which to use primary (0.0 to 1.0)
Session Management
SessionManager
Manages NEAR AI authentication sessions.Create a new session manager
Get current session token or create new session
Refresh the current session
SessionConfig
Where to store session data
NEAR AI auth endpoint
Error Handling
LlmError
Authentication failed
API request failed
Rate limit exceeded
Response parsing failed
Request exceeds model context window
Circuit breaker is open
Cost Tracking
TokenUsage
Tracks token usage and costs.Tokens in prompts
Tokens in completions
Total cost in USD
Related Modules
Agent Module
Agent reasoning and tool selection using LLMs
Workspace Module
Workspace system prompts and memory context