litellm

package module
v1.6.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: Apache-2.0 Imports: 20 Imported by: 2

README

LiteLLM (Go) — Multi‑Provider LLM Client

中文 | English

LiteLLM is a small, typed Go client that lets you call multiple LLM providers through one API.

Get Started

Install
go get github.com/voocel/litellm
1) Prepare an API key
export OPENAI_API_KEY="your-key"
2) Quick examples (minimal runnable)
Text (chat)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/voocel/litellm"
)

func main() {
	client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
		APIKey: os.Getenv("OPENAI_API_KEY"),
	})
	if err != nil {
		log.Fatal(err)
	}

	resp, err := client.Chat(context.Background(), &litellm.Request{
		Model:    "gpt-4o-mini",
		Messages: []litellm.Message{litellm.UserMessage("Explain AI in one sentence.")},
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Content)
}
Tool calling
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/voocel/litellm"
)

func main() {
	client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
		APIKey: os.Getenv("OPENAI_API_KEY"),
	})
	if err != nil {
		log.Fatal(err)
	}

	tools := []litellm.Tool{
		litellm.NewTool("get_weather", "Get weather for a city.", map[string]any{
			"type": "object",
			"properties": map[string]any{
				"city": map[string]any{"type": "string"},
			},
			"required": []string{"city"},
		}),
	}

	resp, err := client.Chat(context.Background(), &litellm.Request{
		Model:      "gpt-4o-mini",
		Messages:   []litellm.Message{litellm.UserMessage("Weather in Tokyo?")},
		Tools:      tools,
		ToolChoice: "auto",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Content)
}
Streaming (collect)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/voocel/litellm"
)

func main() {
	client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
		APIKey: os.Getenv("OPENAI_API_KEY"),
	})
	if err != nil {
		log.Fatal(err)
	}

	stream, err := client.Stream(context.Background(), &litellm.Request{
		Model:    "gpt-4o-mini",
		Messages: []litellm.Message{litellm.UserMessage("Tell me a joke.")},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()

	resp, err := litellm.CollectStream(stream)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Content)
}

If you need real-time streaming and a final aggregated response:

resp, err := litellm.CollectStreamWithHandler(stream, func(chunk *litellm.StreamChunk) {
	if chunk.Type == litellm.ChunkTypeContent && chunk.Content != "" {
		fmt.Print(chunk.Content)
	}
	if chunk.ReasoningDone {
		fmt.Print("\n[reasoning done]")
	}
})
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n---")
fmt.Println(resp.Content)

Notes

  • The providers package is an internal implementation detail. End users should only import github.com/voocel/litellm.
  • LiteLLM does not auto-discover providers or auto-route models. You must configure providers explicitly.

Core API

  • New(provider, opts...) builds a client with an explicit provider.
  • NewWithProvider(name, config, opts...) builds a client from a provider name and config.
  • Request is provider‑agnostic: set Model and Messages, then optional controls like MaxTokens, Temperature, TopP, Stop, etc.
  • Chat(ctx, req) returns a unified Response.
  • Stream(ctx, req) returns a StreamReader (not goroutine‑safe). Always defer stream.Close().
  • CollectStream(stream) collects a stream into a unified Response.
  • CollectStreamWithHandler(stream, onChunk) collects and also handles each chunk.
  • CollectStreamWithCallbacks(stream, callbacks) adds content/reasoning/tool callbacks.
  • WithHook(h) / WithHooks(...) observe requests, responses, and stream chunks without changing control flow.
  • Request.Thinking controls thinking output when explicitly set; otherwise behavior is provider-specific.
  • ListModels(ctx) lists available models for the current provider (only some providers; fields are best‑effort).
Streaming (minimal)
stream, err := client.Stream(ctx, &litellm.Request{
	Model: "gpt-4o-mini",
	Messages: []litellm.Message{
		{Role: "user", Content: "Tell me a joke."},
	},
})
if err != nil {
	log.Fatal(err)
}
defer stream.Close()

resp, err := litellm.CollectStream(stream)
if err != nil {
	log.Fatal(err)
}
fmt.Print(resp.Content)
Hooks

Use hooks when you want lightweight observability around requests without modifying provider behavior.

client, err := litellm.NewWithProvider(
	"openai",
	litellm.ProviderConfig{APIKey: os.Getenv("OPENAI_API_KEY")},
	litellm.WithHook(litellm.HookFuncs{
		BeforeRequestFunc: func(ctx context.Context, meta litellm.CallMeta) {
			log.Printf("→ call=%s %s provider=%s model=%s", meta.CallID, meta.Operation, meta.Provider, meta.Model)
		},
		AfterResponseFunc: func(ctx context.Context, meta litellm.CallMeta, resp *litellm.Response, err error) {
			if err != nil {
				log.Printf("← call=%s %s error=%v", meta.CallID, meta.Operation, err)
				return
			}
			log.Printf("← call=%s %s duration=%s", meta.CallID, meta.Operation, meta.Duration)
		},
		OnStreamChunkFunc: func(ctx context.Context, meta litellm.CallMeta, chunk *litellm.StreamChunk) {
			if chunk.Type == litellm.ChunkTypeContent && chunk.Content != "" {
				fmt.Print(chunk.Content)
			}
		},
	}),
)
if err != nil {
	log.Fatal(err)
}

Notes:

  • Hooks are observational. They do not modify requests, responses, or stream flow.
  • CallMeta.CallID is stable for the lifetime of a single call and can be used to correlate hook events.
  • For streaming calls, AfterResponse runs when the stream is established; resp is nil in that case.

Advanced Features (optional)

Each feature below works across providers. Longer runnable examples live in examples/.

Model listing (some providers)

Notes

  • Supported today: OpenAI / Anthropic / Gemini / OpenRouter / DeepSeek / Bedrock
  • Returned fields vary by provider; ModelInfo is best‑effort
  • Gemini model IDs are normalized (the models/ prefix is removed)
  • Bedrock control plane can be overridden via ProviderConfig.Extra["control_plane_base_url"]
models, err := client.ListModels(ctx)
if err != nil {
	log.Fatal(err)
}
for _, m := range models {
	fmt.Println(m.ID, m.Name)
}
Structured outputs
schema := map[string]any{
	"type": "object",
	"properties": map[string]any{
		"name": map[string]any{"type": "string"},
		"age":  map[string]any{"type": "integer"},
	},
	"required": []string{"name", "age"},
}

resp, err := client.Chat(ctx, &litellm.Request{
	Model: "gpt-4o-mini",
	Messages: []litellm.Message{{Role: "user", Content: "Generate a person."}},
	ResponseFormat: litellm.NewResponseFormatJSONSchema("person", "", schema, true),
})
_ = resp
Function calling
tools := []litellm.Tool{
	{
		Type: "function",
		Function: litellm.FunctionDef{
			Name: "get_weather",
			Parameters: map[string]any{
				"type": "object",
				"properties": map[string]any{
					"city": map[string]any{"type": "string"},
				},
				"required": []string{"city"},
			},
		},
	},
}

resp, err := client.Chat(ctx, &litellm.Request{
	Model: "gpt-4o-mini",
	Messages: []litellm.Message{{Role: "user", Content: "Weather in Tokyo?"}},
	Tools: tools,
	ToolChoice: "auto",
})
_ = resp
Thinking output
resp, err := client.Chat(ctx, &litellm.Request{
	Model:    "claude-haiku-4-5-20251001",
	Messages: []litellm.Message{litellm.UserMessage("Explain the tradeoffs.")},
	Thinking: litellm.NewThinkingEnabled(1024),
})
_ = resp

To disable:

req := &litellm.Request{
	Model:    "claude-haiku-4-5-20251001",
	Messages: []litellm.Message{litellm.UserMessage("Explain the tradeoffs.")},
	Thinking: litellm.NewThinkingDisabled(),
}
_ = req
OpenAI Responses API
resp, err := client.Responses(ctx, &litellm.OpenAIResponsesRequest{
	Model: "o3-mini",
	Messages: []litellm.Message{{Role: "user", Content: "Solve 15*8 step by step."}},
	ReasoningEffort:  "medium",
	ReasoningSummary: "auto",
	Thinking:         litellm.NewThinkingEnabled(0),
	MaxOutputTokens:  litellm.IntPtr(800),
})
_ = resp
Retries & timeouts
res := litellm.DefaultResilienceConfig()
res.MaxRetries = 3
res.InitialDelay = 1 * time.Second
res.RequestTimeout = 60 * time.Second

client, _ := litellm.NewWithProvider("openai", litellm.ProviderConfig{
	APIKey:     os.Getenv("OPENAI_API_KEY"),
	Resilience: res,
})
_ = client
Provider‑specific knobs

Request.Extra is validated per provider. Unsupported providers will return an error.

Supported keys:

  • Gemini: tool_name (string) for tool response naming
Cost calculation

Calculate request costs based on token usage. Pricing data is fetched from BerriAI/litellm and loaded automatically on first use.

resp, err := client.Chat(ctx, req)
if err != nil {
	log.Fatal(err)
}

// Calculate cost (pricing data loads automatically)
if cost, err := litellm.CalculateCostForResponse(resp); err == nil {
	fmt.Printf("Cost: $%.6f (input: $%.6f, output: $%.6f)\n",
		cost.TotalCost, cost.InputCost, cost.OutputCost)
}

// Or use the standalone function
cost, err := litellm.CalculateCost(resp.Model, resp.Usage)

// Set custom pricing for unlisted models
litellm.SetModelPricing("my-model", litellm.ModelPricing{
	InputCostPerToken:  0.000001,
	OutputCostPerToken: 0.000002,
})

Custom Providers

Implement litellm.Provider and register it:

type MyProvider struct {
	name   string
	config litellm.ProviderConfig
}

func (p *MyProvider) Name() string                     { return p.name }
func (p *MyProvider) Validate() error                 { return nil }

func (p *MyProvider) Chat(ctx context.Context, req *litellm.Request) (*litellm.Response, error) {
	return &litellm.Response{Content: "hello", Model: req.Model, Provider: p.name}, nil
}
func (p *MyProvider) Stream(ctx context.Context, req *litellm.Request) (litellm.StreamReader, error) {
	return nil, fmt.Errorf("streaming not implemented")
}

func init() {
	_ = litellm.RegisterProviderWithDescriptor(litellm.ProviderDescriptor{
		Name:       "myprovider",
		DefaultURL: "https://api.example.com/v1",
		Factory: func(cfg litellm.ProviderConfig) litellm.Provider {
			return &MyProvider{name: "myprovider", config: cfg}
		},
	})
}

Custom provider contract:

  • Chat should validate basic request fields and return canonical fields when possible: Response.Provider, Response.Model, Response.FinishReason, and Response.Usage.
  • Stream should emit text deltas as ChunkTypeContent, reasoning deltas via ReasoningContent, tool deltas via ToolCallDelta, and finish with a chunk where Done=true.
  • ToolCallDelta.Index must stay stable for the same streamed tool call so the accumulator can rebuild complete tool calls.
  • Prefer returning LiteLLMError values, or errors wrapped by WrapError, so upper layers can classify auth, rate limit, timeout, model, and validation failures consistently.

Supported Providers

Builtin providers: OpenAI, Anthropic, Google Gemini, DeepSeek, Qwen (DashScope), GLM, AWS Bedrock, OpenRouter.

LiteLLM does not rewrite model IDs. Always use official model IDs.

Configuration

Configure providers explicitly:

client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
	APIKey:  os.Getenv("OPENAI_API_KEY"),
	BaseURL: os.Getenv("OPENAI_BASE_URL"), // optional
})
_ = client

License

Apache License

Documentation

Overview

Package litellm provides a predictable, explicit client for multiple LLM providers.

Design Principles

  • Explicit configuration: no environment auto-discovery, no auto-routing
  • Single-provider client: each Client binds exactly one Provider
  • Predictable behavior: fail fast instead of guessing

Quick Start

Create a client explicitly:

client, err := litellm.NewWithProvider("openai", litellm.ProviderConfig{
    APIKey: os.Getenv("OPENAI_API_KEY"),
})
if err != nil {
    log.Fatal(err)
}

resp, err := client.Chat(context.Background(), &litellm.Request{
    Model: "gpt-4o-mini",
    Messages: []litellm.Message{
        {Role: "user", Content: "Explain AI in one sentence."},
    },
})

Streaming

stream, err := client.Stream(ctx, &litellm.Request{
    Model: "gpt-4o-mini",
    Messages: []litellm.Message{
        {Role: "user", Content: "Tell me a joke."},
    },
})
if err != nil {
    log.Fatal(err)
}
defer stream.Close()

for {
    chunk, err := stream.Next()
    if err != nil || chunk.Done {
        break
    }
    fmt.Print(chunk.Content)
}

Or collect the full response:

resp, err := litellm.CollectStream(stream)
if err != nil {
    log.Fatal(err)
}
fmt.Print(resp.Content)

Or stream while aggregating:

resp, err := litellm.CollectStreamWithHandler(stream, func(chunk *litellm.StreamChunk) {
    if chunk.Type == litellm.ChunkTypeContent && chunk.Content != "" {
        fmt.Print(chunk.Content)
    }
})
if err != nil {
    log.Fatal(err)
}
fmt.Print(resp.Content)

OpenAI Responses API

Use a dedicated request type for Responses API:

resp, err := client.Responses(ctx, &litellm.OpenAIResponsesRequest{
    Model: "o3-mini",
    Messages: []litellm.Message{
        {Role: "user", Content: "Solve 15*8 step by step."},
    },
    ReasoningEffort:  "medium",
    ReasoningSummary: "auto",
    MaxOutputTokens:  litellm.IntPtr(800),
})
_ = resp

Custom Providers

Implement the Provider interface and register it:

type MyProvider struct{}
func (p *MyProvider) Name() string { return "myprovider" }
func (p *MyProvider) Validate() error { return nil }
func (p *MyProvider) Chat(ctx context.Context, req *litellm.Request) (*litellm.Response, error) { ... }
func (p *MyProvider) Stream(ctx context.Context, req *litellm.Request) (litellm.StreamReader, error) { ... }

litellm.RegisterProviderWithDescriptor(litellm.ProviderDescriptor{
    Name:       "myprovider",
    DefaultURL: "https://api.example.com/v1",
    Factory: func(cfg litellm.ProviderConfig) litellm.Provider {
        return &MyProvider{}
    },
})

Custom provider contract:

  • Chat should validate request basics and return canonical fields when possible: Response.Provider, Response.Model, Response.FinishReason, and Response.Usage.
  • Stream should emit ChunkTypeContent for text deltas, ReasoningContent for reasoning deltas, ToolCallDelta for tool streaming, and a final chunk with Done=true.
  • ToolCallDelta.Index must remain stable across all deltas of the same tool call.
  • Prefer returning LiteLLMError (or errors wrapped by WrapError) so upper layers can classify auth, rate-limit, timeout, model, and validation failures consistently.

Thread Safety

Client is safe for concurrent use. StreamReader is not goroutine-safe and must be consumed by a single goroutine.

Index

Constants

View Source
const (
	CacheTTL5m = "5m"
	CacheTTL1h = "1h"
)

Cache TTL constants for Anthropic ephemeral cache_control.

View Source
const (
	ChunkTypeContent       = "content"
	ChunkTypeToolCallDelta = "tool_call_delta"
	ChunkTypeReasoning     = "reasoning"
)

Stream chunk type constants.

View Source
const (
	ChunkTypeContentStart   = "content_start"
	ChunkTypeContentEnd     = "content_end"
	ChunkTypeReasoningStart = "reasoning_start"
	ChunkTypeReasoningEnd   = "reasoning_end"
	ChunkTypeToolCallStart  = "tool_call_start"
	ChunkTypeToolCallEnd    = "tool_call_end"
)

Lifecycle event types — bracket start/end of each content block during streaming.

View Source
const (
	ResponseFormatText       = "text"
	ResponseFormatJSONObject = "json_object"
	ResponseFormatJSONSchema = "json_schema"
)

ResponseFormat type constants.

View Source
const (
	ThinkingEnabled  = "enabled"
	ThinkingDisabled = "disabled"
)

Thinking type constants.

View Source
const (
	FinishReasonStop     = providers.FinishReasonStop
	FinishReasonLength   = providers.FinishReasonLength
	FinishReasonToolCall = providers.FinishReasonToolCall
	FinishReasonError    = providers.FinishReasonError
	FinishReasonSafety   = providers.FinishReasonSafety
)

FinishReason constants — canonical values returned by all providers.

View Source
const (
	CacheTypeEphemeral = "ephemeral"
)

CacheControl type constants. Anthropic Messages API only defines "ephemeral"; retention is controlled via the optional ttl field ("5m" / "1h").

View Source
const PricingURL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"

Variables

View Source
var ErrStreamIdle = errors.New("stream idle timeout: no chunks received")

ErrStreamIdle is the sentinel cause for stream idle-timeout errors. Use errors.Is(err, ErrStreamIdle) or IsStreamIdleError(err) to detect.

Functions

func BoolPtr added in v1.2.1

func BoolPtr(v bool) *bool

BoolPtr returns a pointer to a bool value Example: enabled := litellm.BoolPtr(true)

func Float64Ptr

func Float64Ptr(v float64) *float64

Float64Ptr returns a pointer to a float64 value Example: req.Temperature = litellm.Float64Ptr(0.7)

func GetRetryAfter added in v1.5.0

func GetRetryAfter(err error) int

func IntPtr

func IntPtr(v int) *int

IntPtr returns a pointer to an int value Example: req.MaxTokens = litellm.IntPtr(2048)

func IsAuthError added in v1.5.0

func IsAuthError(err error) bool

func IsContextOverflowError added in v1.6.0

func IsContextOverflowError(err error) bool

func IsModelError added in v1.5.0

func IsModelError(err error) bool

func IsNetworkError added in v1.5.0

func IsNetworkError(err error) bool

func IsOverloadedError added in v1.6.3

func IsOverloadedError(err error) bool

func IsPricingLoaded added in v1.5.5

func IsPricingLoaded() bool

IsPricingLoaded returns whether registry data has been loaded.

func IsProviderRegistered added in v1.5.0

func IsProviderRegistered(name string) bool

IsProviderRegistered checks if a provider is registered (built-in or custom)

func IsRateLimitError added in v1.5.0

func IsRateLimitError(err error) bool

func IsRetryableError added in v1.5.0

func IsRetryableError(err error) bool

func IsStreamIdleError added in v1.6.8

func IsStreamIdleError(err error) bool

IsStreamIdleError reports whether err originated from a stream idle-timeout abort.

func IsValidationError added in v1.5.0

func IsValidationError(err error) bool

func ListRegisteredProviders added in v1.5.0

func ListRegisteredProviders() []string

ListRegisteredProviders returns all registered provider names

func LoadPricing added in v1.5.5

func LoadPricing(ctx context.Context) error

LoadPricing fetches model registry data from BerriAI/litellm GitHub.

func LoadPricingFromReader added in v1.5.5

func LoadPricingFromReader(r io.Reader) error

LoadPricingFromReader loads model registry data from any io.Reader.

func NormalizeFinishReason added in v1.5.7

func NormalizeFinishReason(raw string) string

NormalizeFinishReason maps provider-specific stop reasons to canonical constants.

func ParsePartialJSON added in v1.6.0

func ParsePartialJSON(data string) any

ParsePartialJSON attempts to parse potentially incomplete JSON from streaming. It tries standard parsing first, then attempts to complete truncated JSON by closing open strings, arrays, and objects. Returns nil if the input is empty; returns an empty map if parsing fails entirely.

func RegisterProvider

func RegisterProvider(name string, factory ProviderFactory) error

RegisterProvider registers a custom provider factory Returns an error if the name is empty or factory is nil

func RegisterProviderWithDescriptor added in v1.6.4

func RegisterProviderWithDescriptor(descriptor ProviderDescriptor) error

RegisterProviderWithDescriptor registers a custom provider together with lightweight static metadata such as its default BaseURL.

func SetModelPricing added in v1.5.5

func SetModelPricing(model string, pricing ModelPricing)

SetModelPricing sets custom pricing for a model.

func StringPtr added in v1.5.2

func StringPtr(v string) *string

StringPtr returns a pointer to a string value Example: req.User = litellm.StringPtr("user-123")

func WrapError added in v1.5.0

func WrapError(err error, provider string) error

Types

type CacheControl added in v1.5.0

type CacheControl = providers.CacheControl

Core types are sourced from providers; litellm re-exports them.

func NewEphemeralCache added in v1.5.0

func NewEphemeralCache() *CacheControl

NewEphemeralCache creates an ephemeral cache_control breakpoint with the provider default TTL (~5 minutes for Anthropic).

func NewEphemeralCacheTTL added in v1.6.10

func NewEphemeralCacheTTL(ttl string) *CacheControl

NewEphemeralCacheTTL creates an ephemeral cache_control breakpoint with an explicit TTL. Per Anthropic Messages API, accepted values are "5m" (default) and "1h"; other values are passed through as-is and may be rejected by the provider.

type CallMeta added in v1.6.7

type CallMeta struct {
	CallID    string
	Provider  string
	Operation string
	Model     string
	Streaming bool
	StartedAt time.Time
	Duration  time.Duration
}

CallMeta describes a single client call.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a minimal, predictable client bound to a single Provider.

func New

func New(provider Provider, opts ...ClientOption) (*Client, error)

New creates a client with an explicit Provider (no implicit discovery).

func NewWithProvider added in v1.5.4

func NewWithProvider(name string, config ProviderConfig, opts ...ClientOption) (*Client, error)

NewWithProvider creates a client from provider name and config.

func (*Client) Chat added in v1.5.0

func (c *Client) Chat(ctx context.Context, req *Request) (*Response, error)

Chat executes a non-streaming request.

func (*Client) ListModels added in v1.5.5

func (c *Client) ListModels(ctx context.Context) ([]ModelInfo, error)

ListModels returns the list of available models for the bound provider (if supported).

func (*Client) ProviderName added in v1.5.7

func (c *Client) ProviderName() string

ProviderName returns the name of the bound provider.

func (*Client) Responses added in v1.5.4

func (c *Client) Responses(ctx context.Context, req *OpenAIResponsesRequest) (*Response, error)

Responses executes an OpenAI Responses API request on an OpenAI provider.

func (*Client) ResponsesStream added in v1.5.4

func (c *Client) ResponsesStream(ctx context.Context, req *OpenAIResponsesRequest) (StreamReader, error)

ResponsesStream executes a streaming OpenAI Responses API request.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, req *Request) (StreamReader, error)

Stream executes a streaming request.

type ClientOption

type ClientOption func(*Client) error

ClientOption configures the Client.

func WithDebug added in v1.5.5

func WithDebug(enabled bool) ClientOption

WithDebug enables debug logging to stderr.

func WithDebugOutput added in v1.5.5

func WithDebugOutput(w io.Writer) ClientOption

WithDebugOutput enables debug logging to a custom writer. If w is nil, debug logging is disabled.

func WithDefaults

func WithDefaults(maxTokens int, temperature float64, topP float64) ClientOption

WithDefaults sets request-level defaults (applies only when fields are unset).

func WithHook added in v1.6.7

func WithHook(h Hook) ClientOption

WithHook appends a single execution hook to the client.

func WithHooks added in v1.6.7

func WithHooks(hooks ...Hook) ClientOption

WithHooks appends multiple execution hooks to the client.

type CostResult added in v1.5.5

type CostResult struct {
	InputCost      float64 `json:"input_cost"`
	OutputCost     float64 `json:"output_cost"`
	CacheReadCost  float64 `json:"cache_read_cost,omitempty"`
	CacheWriteCost float64 `json:"cache_write_cost,omitempty"`
	TotalCost      float64 `json:"total_cost"`
	Currency       string  `json:"currency"`
}

CostResult contains the calculated cost for a request.

func CalculateCost added in v1.5.5

func CalculateCost(model string, usage Usage) (*CostResult, error)

CalculateCost calculates the cost based on token usage. Registry data is loaded automatically on first call.

func CalculateCostForResponse added in v1.5.5

func CalculateCostForResponse(resp *Response) (*CostResult, error)

CalculateCostForResponse calculates the cost for a response.

type DefaultConfig

type DefaultConfig struct {
	MaxTokens   int     `json:"max_tokens"`
	Temperature float64 `json:"temperature"`
	TopP        float64 `json:"top_p"`
}

DefaultConfig holds request-level defaults.

type ErrorType added in v1.5.0

type ErrorType = providers.ErrorType

Error types and constructors are sourced from providers; this file is a thin re-export.

const (
	ErrorTypeAuth            ErrorType = providers.ErrorTypeAuth
	ErrorTypeRateLimit       ErrorType = providers.ErrorTypeRateLimit
	ErrorTypeNetwork         ErrorType = providers.ErrorTypeNetwork
	ErrorTypeValidation      ErrorType = providers.ErrorTypeValidation
	ErrorTypeProvider        ErrorType = providers.ErrorTypeProvider
	ErrorTypeTimeout         ErrorType = providers.ErrorTypeTimeout
	ErrorTypeQuota           ErrorType = providers.ErrorTypeQuota
	ErrorTypeModel           ErrorType = providers.ErrorTypeModel
	ErrorTypeInternal        ErrorType = providers.ErrorTypeInternal
	ErrorTypeContextOverflow ErrorType = providers.ErrorTypeContextOverflow
	ErrorTypeOverloaded      ErrorType = providers.ErrorTypeOverloaded
)

type FunctionCall

type FunctionCall = providers.FunctionCall

Core types are sourced from providers; litellm re-exports them.

type FunctionDef added in v1.5.0

type FunctionDef = providers.FunctionDef

Core types are sourced from providers; litellm re-exports them.

type Hook added in v1.6.7

type Hook interface {
	BeforeRequest(ctx context.Context, meta CallMeta)
	AfterResponse(ctx context.Context, meta CallMeta, resp *Response, err error)
	OnStreamChunk(ctx context.Context, meta CallMeta, chunk *StreamChunk)
}

Hook observes request execution without modifying control flow. For streaming calls, AfterResponse runs when the stream is established; resp is nil in that case.

type HookFuncs added in v1.6.7

type HookFuncs struct {
	BeforeRequestFunc func(ctx context.Context, meta CallMeta)
	AfterResponseFunc func(ctx context.Context, meta CallMeta, resp *Response, err error)
	OnStreamChunkFunc func(ctx context.Context, meta CallMeta, chunk *StreamChunk)
}

HookFuncs adapts plain functions into a Hook.

func (HookFuncs) AfterResponse added in v1.6.7

func (h HookFuncs) AfterResponse(ctx context.Context, meta CallMeta, resp *Response, err error)

func (HookFuncs) BeforeRequest added in v1.6.7

func (h HookFuncs) BeforeRequest(ctx context.Context, meta CallMeta)

func (HookFuncs) OnStreamChunk added in v1.6.7

func (h HookFuncs) OnStreamChunk(ctx context.Context, meta CallMeta, chunk *StreamChunk)

type JSONSchema added in v1.2.1

type JSONSchema = providers.JSONSchema

Core types are sourced from providers; litellm re-exports them.

type LiteLLMError added in v1.5.0

type LiteLLMError = providers.LiteLLMError

func NewAuthError added in v1.5.0

func NewAuthError(provider, message string) *LiteLLMError

func NewError added in v1.5.0

func NewError(errorType ErrorType, message string) *LiteLLMError

func NewErrorWithCause added in v1.5.0

func NewErrorWithCause(errorType ErrorType, message string, cause error) *LiteLLMError

func NewHTTPError added in v1.5.0

func NewHTTPError(provider string, statusCode int, message string) *LiteLLMError

func NewModelError added in v1.5.0

func NewModelError(provider, model, message string) *LiteLLMError

func NewNetworkError added in v1.5.0

func NewNetworkError(provider, message string, cause error) *LiteLLMError

func NewProviderError added in v1.5.0

func NewProviderError(provider string, errorType ErrorType, message string) *LiteLLMError

func NewRateLimitError added in v1.5.0

func NewRateLimitError(provider, message string, retryAfter int) *LiteLLMError

func NewTimeoutError added in v1.5.0

func NewTimeoutError(provider, message string) *LiteLLMError

func NewValidationError added in v1.5.0

func NewValidationError(provider, message string) *LiteLLMError

type Message

type Message = providers.Message

Core types are sourced from providers; litellm re-exports them.

func AssistantMessage added in v1.5.2

func AssistantMessage(content string) Message

AssistantMessage creates an assistant message Example: litellm.AssistantMessage("Hello! How can I help you?")

func MultiContentMessage added in v1.5.4

func MultiContentMessage(role string, contents ...MessageContent) Message

MultiContentMessage creates a message with multiple content items (text, images, etc).

Example:

msg := litellm.MultiContentMessage("user",
    litellm.TextContent("What's in this image?"),
    litellm.ImageContent("https://example.com/image.png"),
)

func SystemMessage added in v1.5.2

func SystemMessage(content string) Message

SystemMessage creates a system message Example: litellm.SystemMessage("You are a helpful assistant.")

func ToolMessage added in v1.5.2

func ToolMessage(toolCallID, content string) Message

ToolMessage creates a tool response message Example: litellm.ToolMessage("call_abc123", `{"result": "success"}`)

func UserMessage added in v1.5.2

func UserMessage(content string) Message

UserMessage creates a user message Example: litellm.UserMessage("Hello, AI!")

type MessageContent added in v1.5.3

type MessageContent = providers.MessageContent

Core types are sourced from providers; litellm re-exports them.

func ImageContent added in v1.5.4

func ImageContent(url string) MessageContent

ImageContent creates an image content item from a URL.

func ImageContentWithDetail added in v1.5.4

func ImageContentWithDetail(url, detail string) MessageContent

ImageContentWithDetail creates an image content item with detail level. Detail can be "auto", "low", or "high".

func TextContent added in v1.5.4

func TextContent(text string) MessageContent

TextContent creates a text content item for multi-content messages.

func ToolRefContent added in v1.6.2

func ToolRefContent(toolName string) MessageContent

ToolRefContent creates a tool_reference content item for tool search results.

type MessageImageURL added in v1.5.3

type MessageImageURL = providers.MessageImageURL

Core types are sourced from providers; litellm re-exports them.

type ModelCapabilities added in v1.5.7

type ModelCapabilities struct {
	Provider          string `json:"litellm_provider"`
	MaxInputTokens    int    `json:"max_input_tokens"`
	MaxOutputTokens   int    `json:"max_output_tokens"`
	SupportsTools     bool   `json:"supports_function_calling"`
	SupportsVision    bool   `json:"supports_vision"`
	SupportsReasoning bool   `json:"supports_reasoning"`
}

ModelCapabilities contains capability and limit metadata for a model.

func GetModelCapabilities added in v1.5.7

func GetModelCapabilities(model string) (*ModelCapabilities, bool)

GetModelCapabilities returns capability metadata for a model.

type ModelInfo

type ModelInfo = providers.ModelInfo

Core types are sourced from providers; litellm re-exports them.

type ModelLister added in v1.5.5

type ModelLister = providers.ModelLister

Core types are sourced from providers; litellm re-exports them.

type ModelPricing added in v1.5.5

type ModelPricing struct {
	InputCostPerToken      float64 `json:"input_cost_per_token"`
	OutputCostPerToken     float64 `json:"output_cost_per_token"`
	CacheReadCostPerToken  float64 `json:"cache_read_input_token_cost,omitempty"`
	CacheWriteCostPerToken float64 `json:"cache_creation_input_token_cost,omitempty"`
}

ModelPricing contains pricing information for a model.

func GetModelPricing added in v1.5.5

func GetModelPricing(model string) (*ModelPricing, bool)

GetModelPricing returns the pricing for a model.

type OpenAIResponsesRequest added in v1.5.4

type OpenAIResponsesRequest = providers.OpenAIResponsesRequest

Core types are sourced from providers; litellm re-exports them.

type OpenAIResponsesTool added in v1.6.9

type OpenAIResponsesTool = providers.OpenAIResponsesTool

Core types are sourced from providers; litellm re-exports them.

type Provider

type Provider = providers.Provider

Core types are sourced from providers; litellm re-exports them.

type ProviderConfig

type ProviderConfig = providers.ProviderConfig

Core types are sourced from providers; litellm re-exports them.

type ProviderDescriptor added in v1.6.4

type ProviderDescriptor struct {
	Name       string
	DefaultURL string
	Factory    ProviderFactory
}

ProviderDescriptor describes a custom provider registration. Keep this intentionally small: only static metadata needed by the client runtime should live here.

type ProviderFactory

type ProviderFactory func(config ProviderConfig) Provider

ProviderFactory is used to register custom providers.

type Request

type Request = providers.Request

Core types are sourced from providers; litellm re-exports them.

func NewRequest added in v1.5.4

func NewRequest(model, prompt string, opts ...RequestOption) *Request

NewRequest creates a new Request with the given model and user prompt. Additional options can be passed to configure the request.

Example:

req := litellm.NewRequest("gpt-4", "Hello",
    litellm.WithSystemPrompt("You are helpful"),
    litellm.WithMaxTokens(1024),
    litellm.WithTemperature(0.7),
)

func NewRequestWithMessages added in v1.5.4

func NewRequestWithMessages(model string, messages []Message, opts ...RequestOption) *Request

NewRequestWithMessages creates a new Request with the given model and messages. Use this when you need full control over the message history.

Example:

req := litellm.NewRequestWithMessages("gpt-4",
    []litellm.Message{
        litellm.SystemMessage("You are helpful"),
        litellm.UserMessage("Hello"),
    },
    litellm.WithMaxTokens(1024),
)

type RequestOption added in v1.5.4

type RequestOption func(*Request)

RequestOption configures a Request using the functional options pattern.

func WithCacheRetention added in v1.6.0

func WithCacheRetention(retention string) RequestOption

WithCacheRetention enables automatic prompt-caching placement.

Default behavior (option unset): no automatic caching — callers are expected to attach cache_control to specific messages themselves. This is opt-in by design so application frameworks can control exactly which segments use the scarce per-request marker budget (Anthropic caps at 4).

Accepted values (case-insensitive, applied per-provider):

  • "" / "none" : disable auto-caching (default)
  • "short" / "5m" : ephemeral cache, ~5-minute TTL (provider default)
  • "long" / "1h" : ephemeral cache, 1-hour TTL where supported

Provider behavior when enabled:

  • Anthropic / OpenRouter→Anthropic: places cache_control on each system content block that doesn't already carry one, and on the last user message's last content block. User-provided CacheControl on individual messages always takes precedence.
  • Bedrock (Anthropic models, Converse API): appends cachePoint markers after the last system block, last user message content block, and tools section.
  • OpenAI / Azure OpenAI: prefer prompt_cache_retention via Extra ("in_memory" or "24h"); cache_control breakpoints are not applicable.

Other providers ignore this option.

func WithExtra added in v1.5.4

func WithExtra(key string, value any) RequestOption

WithExtra sets a provider-specific parameter.

func WithExtras added in v1.5.4

func WithExtras(extras map[string]any) RequestOption

WithExtras sets multiple provider-specific parameters.

func WithJSONMode added in v1.5.4

func WithJSONMode() RequestOption

WithJSONMode enables JSON object output mode. The model will return valid JSON without enforcing a specific schema.

func WithJSONSchema added in v1.5.4

func WithJSONSchema(name, description string, schema any, strict bool) RequestOption

WithJSONSchema enables structured JSON output with schema validation.

func WithMaxTokens added in v1.5.4

func WithMaxTokens(n int) RequestOption

WithMaxTokens sets the maximum number of tokens to generate.

func WithOnPayload added in v1.6.0

func WithOnPayload(fn func(provider string, payload []byte)) RequestOption

WithOnPayload sets a debug hook that receives the serialized JSON body before each HTTP request is sent to the provider.

func WithOnWarning added in v1.6.8

func WithOnWarning(fn func(provider string, message string)) RequestOption

WithOnWarning sets a hook for portability warnings, such as an unsupported option being omitted for a provider instead of failing the request.

func WithResponseFormat added in v1.5.4

func WithResponseFormat(format *ResponseFormat) RequestOption

WithResponseFormat sets a custom response format.

func WithStop added in v1.5.4

func WithStop(sequences ...string) RequestOption

WithStop sets the stop sequences.

func WithSystemPrompt added in v1.5.4

func WithSystemPrompt(content string) RequestOption

WithSystemPrompt prepends a system message to the request. If a system message already exists, it will be replaced.

func WithTemperature added in v1.5.4

func WithTemperature(t float64) RequestOption

WithTemperature sets the sampling temperature (0.0 to 2.0). Higher values make output more random, lower values more deterministic.

func WithThinking added in v1.5.4

func WithThinking(opts ...any) RequestOption

WithThinking enables thinking/reasoning mode.

WithThinking()              // enable with provider defaults
WithThinking("high")        // set reasoning effort level (low/medium/high)
WithThinking(8192)          // set token budget
WithThinking("high", 8192)  // both level and budget

func WithToolChoice added in v1.5.4

func WithToolChoice(choice any) RequestOption

WithToolChoice sets the tool choice behavior. Accepts: "auto", "none", "required", or a specific tool name.

func WithTools added in v1.5.4

func WithTools(tools ...Tool) RequestOption

WithTools adds tool definitions to the request.

func WithTopP added in v1.5.4

func WithTopP(p float64) RequestOption

WithTopP sets the nucleus sampling parameter (0.0 to 1.0).

func WithoutThinking added in v1.5.4

func WithoutThinking() RequestOption

WithoutThinking explicitly disables thinking/reasoning mode.

type ResilienceConfig added in v1.2.2

type ResilienceConfig = providers.ResilienceConfig

ResilienceConfig and defaults are sourced from providers; re-exported here to keep the public API small.

func DefaultResilienceConfig added in v1.2.2

func DefaultResilienceConfig() ResilienceConfig

type ResilientHTTPClient added in v1.2.2

type ResilientHTTPClient struct {
	// contains filtered or unexported fields
}

ResilientHTTPClient wraps http.Client with retry logic

func NewResilientHTTPClient added in v1.2.2

func NewResilientHTTPClient(config ResilienceConfig) *ResilientHTTPClient

NewResilientHTTPClient creates a new resilient HTTP client

func (*ResilientHTTPClient) Do added in v1.2.2

Do executes HTTP request with retry logic

type Response

type Response = providers.Response

Core types are sourced from providers; litellm re-exports them.

func CollectStream added in v1.5.4

func CollectStream(stream StreamReader) (*Response, error)

CollectStream consumes a StreamReader and returns a unified Response. Callers are responsible for closing the stream.

func CollectStreamWithCallbacks added in v1.5.4

func CollectStreamWithCallbacks(stream StreamReader, callbacks StreamCallbacks) (*Response, error)

CollectStreamWithCallbacks consumes a StreamReader, calls callbacks for each chunk, and returns a unified Response. Callers are responsible for closing the stream.

func CollectStreamWithHandler added in v1.5.4

func CollectStreamWithHandler(stream StreamReader, onChunk func(*StreamChunk)) (*Response, error)

CollectStreamWithHandler consumes a StreamReader, calls onChunk for each chunk, and returns a unified Response. Callers are responsible for closing the stream.

type ResponseFormat added in v1.2.1

type ResponseFormat = providers.ResponseFormat

Core types are sourced from providers; litellm re-exports them.

func NewResponseFormatJSONObject added in v1.2.1

func NewResponseFormatJSONObject() *ResponseFormat

NewResponseFormatJSONObject creates a JSON object response format This ensures the model returns valid JSON without enforcing a specific schema

func NewResponseFormatJSONSchema added in v1.2.1

func NewResponseFormatJSONSchema(name, description string, schema any, strict bool) *ResponseFormat

NewResponseFormatJSONSchema creates a JSON schema response format with strict validation enabled/disabled

Parameters:

  • name: Schema name (required)
  • description: Schema description (optional, can be empty)
  • schema: JSON Schema definition as a map[string]interface{}
  • strict: Enable strict schema validation (OpenAI only)

Example:

schema := map[string]interface{}{
    "type": "object",
    "properties": map[string]interface{}{
        "name": map[string]interface{}{"type": "string"},
        "age": map[string]interface{}{"type": "integer"},
    },
    "required": []string{"name", "age"},
}
format := litellm.NewResponseFormatJSONSchema("person", "A person object", schema, true)

func NewResponseFormatText added in v1.2.1

func NewResponseFormatText() *ResponseFormat

NewResponseFormatText creates a text response format

type StreamCallbacks added in v1.5.4

type StreamCallbacks struct {
	OnChunk     func(*StreamChunk)
	OnContent   func(string)
	OnReasoning func(content string)
	OnToolCall  func(*ToolCallDelta)

	// Lifecycle callbacks — bracket start/end of each content block.
	// Transitions are detected automatically from chunk types.
	OnContentStart   func()
	OnContentEnd     func(content string) // content = full accumulated block
	OnReasoningStart func()
	OnReasoningEnd   func(content string)       // content = full accumulated reasoning
	OnToolCallStart  func(delta *ToolCallDelta) // carries ID and FunctionName
	OnToolCallEnd    func(call ToolCall)        // carries complete ToolCall
}

StreamCallbacks provides optional per-chunk handlers during stream collection.

type StreamChunk

type StreamChunk = providers.StreamChunk

Core types are sourced from providers; litellm re-exports them.

type StreamReader

type StreamReader = providers.StreamReader

Core types are sourced from providers; litellm re-exports them.

type ThinkingConfig added in v1.5.4

type ThinkingConfig = providers.ThinkingConfig

Core types are sourced from providers; litellm re-exports them.

func NewThinkingDisabled added in v1.5.4

func NewThinkingDisabled() *ThinkingConfig

NewThinkingDisabled disables thinking output.

func NewThinkingEnabled added in v1.5.4

func NewThinkingEnabled(budgetTokens int) *ThinkingConfig

NewThinkingEnabled enables thinking with an optional budget.

func NewThinkingWithLevel added in v1.5.7

func NewThinkingWithLevel(level string) *ThinkingConfig

NewThinkingWithLevel enables thinking with a reasoning level. Level is provider-specific: "low", "medium", "high" etc.

type Tool

type Tool = providers.Tool

Core types are sourced from providers; litellm re-exports them.

func NewTool added in v1.5.4

func NewTool(name, description string, parameters any) Tool

NewTool creates a function tool definition.

type ToolCall

type ToolCall = providers.ToolCall

Core types are sourced from providers; litellm re-exports them.

type ToolCallAccumulator added in v1.5.7

type ToolCallAccumulator struct {
	// contains filtered or unexported fields
}

ToolCallAccumulator reconstructs complete ToolCall objects from streaming deltas. Safe for single-goroutine use only.

func NewToolCallAccumulator added in v1.5.7

func NewToolCallAccumulator() *ToolCallAccumulator

NewToolCallAccumulator creates an empty accumulator.

func (*ToolCallAccumulator) Apply added in v1.5.7

func (a *ToolCallAccumulator) Apply(delta *ToolCallDelta)

Apply processes a single ToolCallDelta, creating or updating the corresponding ToolCall.

func (*ToolCallAccumulator) Build added in v1.5.7

func (a *ToolCallAccumulator) Build() []ToolCall

Build returns the completed ToolCall list in first-seen order.

func (*ToolCallAccumulator) Get added in v1.6.0

func (a *ToolCallAccumulator) Get(index int) *ToolCall

Get returns the accumulated ToolCall for the given index, or nil if not found.

func (*ToolCallAccumulator) PartialArguments added in v1.6.0

func (a *ToolCallAccumulator) PartialArguments(index int) any

PartialArguments returns a best-effort parse of the accumulated (possibly incomplete) function arguments for the tool call at the given index. Useful for streaming UIs that want to display arguments as they arrive. Returns nil if the index has no accumulated data.

func (*ToolCallAccumulator) Started added in v1.5.7

func (a *ToolCallAccumulator) Started(index int) bool

Started reports whether a delta with the given index has been received.

type ToolCallDelta

type ToolCallDelta = providers.ToolCallDelta

Core types are sourced from providers; litellm re-exports them.

type Usage

type Usage = providers.Usage

Core types are sourced from providers; litellm re-exports them.

Directories

Path Synopsis
examples
anthropic command
bedrock command
deepseek command
gemini command
glm command
grok command
ollama command
openai command
openrouter command
qwen command
otel module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL