When calling LLM APIs from Python code. When connecting to llamafile or local LLM servers. When switching between OpenAI/Anthropic/local providers. When implementing retry/fallback logic for LLM calls. When code imports litellm or uses completion() patterns.
When calling LLM APIs from Python code. When connecting to llamafile or local LLM servers. When switching between OpenAI/Anthropic/local providers. When implementing retry/fallback logic for LLM calls. When code imports litellm or uses completion() patterns.
LiteLLM
Unified Python interface for calling 100+ LLM APIs using consistent OpenAI format. Provides standardized exception handling, retry/fallback logic, and cost tracking across multiple providers.
When to Use This Skill
Use this skill when:
Integrating with multiple LLM providers through a single interface
Routing requests to local llamafile servers using OpenAI-compatible endpoints
Implementing retry and fallback logic for LLM calls
Building applications requiring consistent error handling across providers
Tracking LLM usage costs across different providers
Converting between provider-specific APIs and OpenAI format
Deploying LLM proxy servers with unified configuration
Testing applications against both cloud and local LLM endpoints
Core Capabilities
Provider Support
LiteLLM supports 100+ providers through consistent OpenAI-style API:
from litellm import completion
response = completion(
model="llamafile/gemma-3-3b",
messages=[{"role": "user", "content": "Hello"}],
api_base="http://localhost:8080/v1",
num_retries=3, # Retry 3 times on failure
timeout=30.0, # 30 second timeout
)
Proxy Server Configuration
For proxy deployments, use config.yaml:
model_list:-model_name:commit-polish-modellitellm_params:model:llamafile/gemma-3-3b# add llamafile/ prefixapi_base:http://localhost:8080/v1# add api base for OpenAI compatible provider
Application Integration Patterns
Connection Verification Pattern
import litellm
from litellm import APIConnectionError
defverify_llamafile_connection(api_base: str = "http://localhost:8080/v1") -> bool:
"""Check if llamafile server is running."""try:
litellm.completion(
model="llamafile/test",
messages=[{"role": "user", "content": "test"}],
api_base=api_base,
max_tokens=1,
)
returnTrueexcept APIConnectionError:
returnFalse
Async Service Pattern
import litellm
from litellm import acompletion, APIConnectionError
import asyncio
classAIService:
"""LiteLLM wrapper with llamafile routing."""def__init__(self, model: str, api_base: str, temperature: float = 0.3, max_tokens: int = 200):
self.model = model
self.api_base = api_base
self.temperature = temperature
self.max_tokens = max_tokens
asyncdefgenerate_commit_message(self, diff: str, system_prompt: str) -> str:
"""Generate a commit message using the LLM."""try:
response = await acompletion(
model=self.model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Generate a commit message for this diff:\n\n{diff}"},
],
api_base=self.api_base,
temperature=self.temperature,
max_tokens=self.max_tokens,
)
return response.choices[0].message.content.strip()
except APIConnectionError as e:
raise RuntimeError(f"Failed to connect to llamafile server at {self.api_base}: {e.message}")
Common Pitfalls to Avoid
Missing llamafile/ prefix: Without prefix, LiteLLM won't route to OpenAI-compatible endpoint
Wrong port: Llamafile uses 8080 by default, not 8000
Missing /v1 suffix: API base must end with /v1
Adding extra path segments: Do NOT use http://localhost:8080/v1/chat/completions - LiteLLM adds the endpoint path automatically
API key requirement: No API key needed for local llamafile (use empty string or any value if required by validation)
Configuration Examples
TOML Configuration
# ~/.config/commit-polish/config.toml[ai]model = "llamafile/gemma-3-3b"# MUST have llamafile/ prefixtemperature = 0.3max_tokens = 200
llamafile: Activate the llamafile skill using Skill(command: "llamafile") for llamafile server setup, model management, and local LLM deployment patterns
uv: Activate the uv skill using Skill(command: "uv") for Python project management, dependency handling, and virtual environment workflows