import openlit
import litellm
from typing import List, Dict, Any
# Use just 1 line of code, to instantly log your LLM responses across all providers with OpenTelemetry:
litellm.callbacks = ["otel"]
def cost_optimized_completion(
messages: List[Dict[str, str]],
fallback_models: List[str] = None,
max_tokens: int = 100
) -> Dict[str, Any]:
"""
Try models in order of cost efficiency with fallback options
"""
if fallback_models is None:
fallback_models = [
"gpt-3.5-turbo", # Cheapest OpenAI option
"claude-3-haiku-20240307", # Anthropic's fastest/cheapest
"command", # Cohere
"gpt-4o-mini", # OpenAI's smaller model
"gpt-4", # Fallback to premium if needed
]
for i, model in enumerate(fallback_models):
try:
print(f"Attempting {model} (priority {i+1})...")
response = litellm.completion(
model=model,
messages=messages,
max_tokens=max_tokens,
temperature=0.7
)
# Calculate approximate cost (rough estimates)
cost_per_1k_tokens = {
"gpt-3.5-turbo": 0.002,
"gpt-4o-mini": 0.0015,
"gpt-4": 0.03,
"claude-3-haiku-20240307": 0.00025,
"claude-3-sonnet-20240229": 0.003,
"command": 0.015
}
estimated_cost = (response.usage.total_tokens / 1000) * cost_per_1k_tokens.get(model, 0.002)
return {
"success": True,
"model_used": model,
"content": response.choices[0].message.content,
"tokens": response.usage.total_tokens,
"estimated_cost": estimated_cost,
"attempt_number": i + 1
}
except Exception as e:
print(f"Failed with {model}: {e}")
if i == len(fallback_models) - 1: # Last attempt
return {
"success": False,
"error": f"All models failed. Last error: {e}",
"attempts": len(fallback_models)
}
continue
return {"success": False, "error": "No models available"}
# Test cost optimization
result = cost_optimized_completion([
{"role": "user", "content": "Summarize the key benefits of using Docker containers for development"}
])
if result["success"]:
print(f"Success with {result['model_used']} on attempt {result['attempt_number']}")
print(f"Cost: ~${result['estimated_cost']:.4f}, Tokens: {result['tokens']}")
print(f"Response: {result['content'][:100]}...")