Loading...
Loading...
Direct API integration with clean subdomain URLs and Bearer token authentication.
curl -X POST https://api.costlens.dev/integrations/run \
-H "Authorization: Bearer $COSTLENS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"provider":"openai","model":"gpt-4","input":"{\"messages\":[...]}","output":"Hello","tokensUsed":100,"latency":800,"success":true}'All API endpoints are at api.costlens.dev with Bearer token authentication.
Get your API key from Settings → API Keys in your dashboard.
Quick Test:
curl -X POST https://api.costlens.dev/integrations/run \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"provider": "openai", "model": "gpt-4", "input": "Hello", "output": "Hi there!", "tokensUsed": 10, "latency": 500, "success": true}'https://api.costlens.devAll API requests require authentication via API key in the Authorization header:
Authorization: Bearer your_api_key_hereThe /cost-analysis endpoint is under development.
Use /integrations/run to track AI calls and get cost data.
Process multiple AI requests in a single call for 3-5x better performance.
POST https://api.costlens.dev/integrations/batch
Content-Type: application/json
Authorization: Bearer your_api_key_here
{
"runs": [
{
"provider": "openai",
"model": "gpt-4",
"input": "Hello, how are you?",
"output": "I'm doing well, thank you!",
"tokensUsed": 150,
"latency": 1200,
"success": true
},
{
"provider": "anthropic",
"model": "claude-3",
"input": "What's the weather like?",
"output": "I don't have access to real-time weather data.",
"tokensUsed": 200,
"latency": 1000,
"success": true
}
]
}{
"success": true,
"processed": 2,
"errors": 0,
"runs": [
{
"id": "run_123",
"requestId": "req_123",
"provider": "openai",
"model": "gpt-4",
"success": true
}
],
"batchStats": {
"totalRuns": 2,
"successful": 2,
"failed": 0,
"successRate": 100
}
}| Plan | Max Batch Size | Rate Limit |
|---|---|---|
| Free | 10 requests | 5 batches/minute |
| Pro | 50 requests | 20 batches/minute |
| Enterprise | 200 requests | 100 batches/minute |
Track an AI API call.
POST https://api.costlens.dev/integrations/run
Content-Type: application/json
Authorization: Bearer your_api_key_here
{
"provider": "openai",
"model": "gpt-4",
"input": "{\"messages\":[...]}",
"output": "Hello! How can I help you?",
"tokensUsed": 150,
"latency": 1234,
"success": true,
"promptId": "greeting-v1"
}{
"success": true,
"run": {
"id": "run_abc123",
"requestId": "req_123",
"correlationId": "corr_456",
"provider": "openai",
"model": "gpt-4",
"success": true
}
}Get analytics overview.
GET https://api.costlens.dev/analytics/overview?startDate=2025-01-01&endDate=2025-01-31startDate (ISO 8601) - Start date for analyticsendDate (ISO 8601) - End date for analytics{
"totalRuns": 1234,
"totalCost": 45.67,
"totalTokens": 123456,
"avgCostPerRun": 0.037,
"successRate": 98.5,
"avgLatency": 1234,
"dailyData": [
{
"date": "2025-01-15",
"runs": 25,
"cost": 1.25,
"tokens": 2500
}
],
"byModel": [
{
"model": "gpt-4",
"runs": 800,
"cost": 30.50
}
],
"byProvider": [
{
"provider": "openai",
"runs": 1000,
"cost": 35.20
}
]
}Get current usage stats.
GET https://api.costlens.dev/usage{
"plan": "free",
"runsThisMonth": 234,
"runsLimit": 1000,
"percentUsed": 23.4
}CostLens currently supports OpenAI and Anthropic APIs with automatic cost optimization.
Get comprehensive analytics including ML-powered cost forecasting and routing decisions.
GET https://api.costlens.dev/dashboard/stats
Authorization: Bearer your_api_key_here{
"success": true,
"data": {
"totalRuns": 1247,
"totalCost": 45.67,
"savings": { "total": 23.45, "roi": 51 },
"costForecast": {
"next7Days": 12.34,
"next30Days": 52.10,
"confidence": 78,
"trend": "increasing",
"factors": ["Usage trending upward", "Higher weekday usage"]
},
"routingDecisions": {
"total": 1247,
"enforced": 892,
"qualityUpgrades": 45,
"costDowngrades": 310
},
"providerStats": [
{
"provider": "openai",
"requests": 847,
"cost": 32.10,
"avgLatency": 1200,
"successRate": 0.98,
"qualityScore": 0.92
}
]
}
}CostLens provides intelligent routing based on prompt complexity and quality requirements.
POST https://api.costlens.dev/routing/context-aware
Content-Type: application/json
Authorization: Bearer your_api_key_here
{
"prompt": "Summarize this research paper...",
"preferences": {
"priority": "quality",
"maxLatencyMs": 3000,
"qualityRequirement": "high"
}
}{
"success": true,
"decision": {
"provider": "openai",
"model": "gpt-4o-mini",
"reason": "High quality requirement within latency target"
}
}30-day forecast, alerts, and optimization recommendations.
GET https://api.costlens.dev/cost/predictive?windowDays=30&type=forecast
Authorization: Bearer your_api_key_here{
"success": true,
"forecast": {
"projectedMonthlyCost": 123.45,
"trend": "down",
"confidence": 0.88
},
"alerts": [
{ "type": "spend", "level": "warning", "message": "Cost trending up for gpt-4o" }
],
"recommendations": [
"Shift 20% traffic to claude-3-haiku during off-peak",
"Enable model enforcement on critical paths"
]
}Rate limits are per API key with performance optimizations:
| Plan | Requests/Minute | Batch Size |
|---|---|---|
| Free | 60 | 10 runs |
| Starter | 300 | 25 runs |
| Pro | 1,000 | 50 runs |
| Enterprise | 5,000 | 50 runs |
When rate limited, you'll receive:
{
"error": "Rate limit exceeded",
"retryAfter": 60
}| Code | Meaning |
|---|---|
| 401 | Unauthorized - Invalid API key |
| 402 | Payment Required - Upgrade needed |
| 429 | Too Many Requests - Rate limit exceeded |
| 500 | Internal Server Error |
# Track AI Call
curl -X POST https://api.costlens.dev/integrations/run \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"provider": "openai",
"model": "gpt-4",
"input": "{\"messages\":[...]}",
"output": "Hello!",
"tokensUsed": 150,
"latency": 1234,
"success": true
}'
# Batch Processing (NEW - 3-5x faster)
curl -X POST https://api.costlens.dev/integrations/batch \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"runs": [
{
"provider": "openai",
"model": "gpt-4",
"input": "First prompt",
"output": "First response",
"tokensUsed": 150,
"latency": 1200,
"success": true,
"requestId": "req_123"
},
{
"provider": "anthropic",
"model": "claude-3",
"input": "Second prompt",
"output": "Second response",
"tokensUsed": 200,
"latency": 1000,
"success": true,
"requestId": "req_124"
}
]
}'
# Get Analytics
curl -X GET "https://api.costlens.dev/analytics/overview?startDate=2025-01-01&endDate=2025-01-31" \
-H "Authorization: Bearer your_api_key_here"
# Get Usage
curl -X GET https://api.costlens.dev/usage \
-H "Authorization: Bearer your_api_key_here"