Skip to content

Rust SDK Overview

ai-lib-rust (v0.6.6) is the high-performance Rust runtime for the AI-Protocol specification. It implements a protocol-driven architecture where all provider behavior comes from configuration, not code.

The SDK is organized into distinct layers:

The user-facing API:

  • AiClient — Main entry point, created from model identifiers
  • AiClientBuilder — Configuration builder with resilience settings
  • ChatRequestBuilder — Fluent API for building chat requests
  • CallStats — Request/response statistics (tokens, latency)
  • CancelHandle — Graceful stream cancellation

Loads and interprets AI-Protocol manifests:

  • ProtocolLoader — Loads from local files, env vars, or GitHub
  • ProtocolManifest — Parsed provider configuration
  • Validator — JSON Schema validation
  • UnifiedRequest — Standard request format compiled to provider-specific JSON

The heart of streaming processing — an operator-based pipeline:

  • Decoder — Converts byte streams to JSON frames (SSE, JSON Lines)
  • Selector — Filters frames using JSONPath expressions
  • Accumulator — Statefully assembles tool calls from partial chunks
  • FanOut — Expands multi-candidate responses
  • EventMapper — Converts frames to unified StreamingEvent types
  • Retry/Fallback — Pipeline-level retry and fallback operators

HTTP communication:

  • HttpTransport — reqwest-based HTTP client
  • Auth — API key resolution (OS keyring → env vars)
  • Middleware — Transport middleware for logging, metrics

Production reliability patterns:

  • CircuitBreaker — Open/half-open/closed failure isolation
  • RateLimiter — Token bucket algorithm
  • Backpressure — max_inflight semaphore
  • embeddings/ — EmbeddingClient with vector operations
  • cache/ — Response caching with TTL (MemoryCache)
  • batch/ — BatchCollector and BatchExecutor
  • tokens/ — Token counting and cost estimation
  • plugins/ — Plugin trait, registry, hooks, middleware
  • guardrails/ — Content filtering, PII detection
  • routing/ — Model routing and load balancing (feature-gated)
  • telemetry/ — Feedback sink for user feedback collection
CratePurpose
tokioAsync runtime
reqwestHTTP client
serde / serde_json / serde_yamlSerialization
jsonschemaManifest validation
tracingStructured logging
arc-swapHot-reload support
notifyFile watching
keyringOS keyring integration

Optional features enabled via Cargo:

FeatureWhat it enables
routing_mvpCustomModelManager, ModelArray, load balancing strategies
interceptorsInterceptorPipeline for logging, metrics, audit
VariablePurpose
AI_PROTOCOL_DIRProtocol manifest directory
<PROVIDER>_API_KEYProvider API key (e.g., OPENAI_API_KEY)
AI_LIB_RPSRate limit (requests per second)
AI_LIB_BREAKER_FAILURE_THRESHOLDCircuit breaker threshold
AI_LIB_MAX_INFLIGHTMax concurrent requests
AI_HTTP_TIMEOUT_SECSHTTP timeout