This guide covers integrating DeepSeek's API with OwlMetric for comprehensive cost tracking and analytics. Both proxy and direct SDK methods are supported.
This should work wih any OpenAI Compatible Provider.
| Model Family | Models | Token Tracking | Cost Tracking |
|---|---|---|---|
| DeepSeek R1 | deepseek-r1, deepseek-r1-distill-llama-70b | ✅ Full | ✅ Real-time |
| DeepSeek V3 | deepseek-v3, deepseek-v3-base | ✅ Full | ✅ Real-time |
| DeepSeek Coder | deepseek-coder, deepseek-coder-33b | ✅ Full | ✅ Real-time |
Get Your OwlMetric API Key
# From your OwlMetric project dashboard
OWLMETRIC_API_KEY=pk_your_project_key_here
Update Your DeepSeek Client
import OpenAI from 'openai'; // DeepSeek uses OpenAI-compatible API
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://owlmetric.com/api/proxy/deepseek',
defaultHeaders: {
'x-owlmetric': process.env.OWLMETRIC_API_KEY,
}
});
Use as Normal
// All your existing DeepSeek code works unchanged
const completion = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "user", content: "Hello, DeepSeek!" }
]
});
Custom Headers:
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://owlmetric.com/api/proxy',
defaultHeaders: {
'x-owlmetric': process.env.OWLMETRIC_API_KEY,
'x-owlmetric-user-id': 'user-123', // Optional: user tracking
'x-owlmetric-session-id': 'session-456', // Optional: session tracking
}
});
npm install @owlmetric/tracker
import OpenAI from "openai";
import { createTrackedClient } from "@owlmetric/tracker";
const client = createTrackedClient(OpenAI, {
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com",
owlmetricToken: process.env.OWLMETRIC_DEEPSEEK_TOKEN,
provider: "DeepSeek", // Important: specify the provider
});
// Use exactly like the regular OpenAI client
const completion = await client.chat.completions.create({
model: "deepseek-r1",
messages: [{ role: "user", content: "Hello!" }],
});
Simple Conversation:
const response = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "system", content: "You are a helpful coding assistant." },
{ role: "user", content: "Explain async/await in JavaScript." }
],
max_tokens: 300,
temperature: 0.7,
});
console.log(response.choices[0].message.content);
// Costs and tokens automatically tracked in OwlMetric dashboard
Multi-turn Conversation:
const response = await client.chat.completions.create({
model: "deepseek-v3",
messages: [
{ role: "user", content: "What is machine learning?" },
{ role: "assistant", content: "Machine learning is a subset of artificial intelligence..." },
{ role: "user", content: "Can you give me a practical example?" }
],
max_tokens: 200,
});
const stream = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "user", content: "Write a Python function to calculate prime numbers." }
],
stream: true,
max_tokens: 500,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
if (content) process.stdout.write(content);
}
// Final usage statistics tracked when stream completes
const response = await client.chat.completions.create({
model: "deepseek-coder",
messages: [
{ role: "system", content: "You are an expert programmer. Write clean, efficient code with comments." },
{ role: "user", content: "Create a React component for a todo list with add, delete, and toggle functionality." }
],
max_tokens: 800,
temperature: 0.3, // Lower temperature for code generation
});
// Code generation optimized for DeepSeek's strengths
const response = await client.chat.completions.create({
model: "deepseek-r1", // R1 model excels at reasoning
messages: [
{ role: "user", content: "Solve this step by step: If a train travels 120 miles in 2 hours, and then travels 180 miles in 3 hours, what is the average speed for the entire journey?" }
],
max_tokens: 400,
temperature: 0.1, // Very low temperature for mathematical reasoning
});
// Reasoning tokens tracked separately when available
const response = await client.chat.completions.create({
model: "deepseek-v3",
messages: [
{ role: "user", content: "What's the current weather in Beijing?" }
],
functions: [
{
name: "get_weather",
description: "Get current weather for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City name"
}
},
required: ["location"]
}
}
],
function_call: "auto"
});
// Function call tokens tracked separately
# DeepSeek Configuration
DEEPSEEK_API_KEY=sk-your-deepseek-api-key
# OwlMetric Configuration
OWLMETRIC_API_KEY=pk_your_project_key_here
# DeepSeek Configuration
DEEPSEEK_API_KEY=sk-your-deepseek-api-key
# OwlMetric Tracking Token
OWLMETRIC_DEEPSEEK_TOKEN=pt_your_deepseek_tracking_token
OwlMetric automatically tracks detailed token usage for DeepSeek, including reasoning tokens:
{
"prompt_tokens": 85,
"completion_tokens": 156,
"total_tokens": 241,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 45, // DeepSeek R1 specific
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_cache_hit_tokens": 12, // DeepSeek cache system
"prompt_cache_miss_tokens": 73
}
Real-time cost calculation based on current DeepSeek pricing:
DeepSeek's prompt caching is automatically tracked:
const response = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "system", content: "You are a mathematical tutor..." }, // Long system prompt
{ role: "user", content: "Solve this equation: 2x + 5 = 15" }
],
max_tokens: 200,
});
// Cache hit/miss tokens tracked separately for cost optimization
Both methods handle DeepSeek API errors gracefully:
try {
const completion = await client.chat.completions.create({
model: "deepseek-r1",
messages: [{ role: "user", content: "Hello!" }],
});
} catch (error) {
if (error.status === 429) {
console.error('DeepSeek Rate Limit:', error.message);
// Rate limit details tracked in OwlMetric for debugging
} else if (error.status === 401) {
console.error('DeepSeek Auth Error:', error.message);
}
}
Rate Limit Errors:
Error: Rate limit exceeded
Authentication Errors:
Error: Invalid API key
sk-)Model Not Available:
Error: Model not found
Tracking Not Working:
x-owlmetric header is includedpt_...) and provider specificationTest Basic Functionality:
const testResponse = await client.chat.completions.create({
model: "deepseek-v3",
messages: [{ role: "user", content: "Say 'integration working'" }],
max_tokens: 10,
});
console.log('Response:', testResponse.choices[0].message.content);
// Check OwlMetric dashboard for this request
Test Streaming:
const stream = await client.chat.completions.create({
model: "deepseek-r1",
messages: [{ role: "user", content: "Count from 1 to 3" }],
stream: true,
max_tokens: 50,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
if (content) process.stdout.write(content);
}
// Verify final usage appears in dashboard
max_tokens limits// Before
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com",
});
// After (change baseURL and add headers)
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://owlmetric.com/api/proxy/deepseek', // Change this
defaultHeaders: { // Add this
'x-owlmetric': process.env.OWLMETRIC_API_KEY, // Add this
} // Add this
});
// Before (Proxy)
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://owlmetric.com/api/proxy/deepseek',
defaultHeaders: {
'x-owlmetric': process.env.OWLMETRIC_API_KEY,
}
});
// After (SDK)
import { createTrackedClient } from "@owlmetric/tracker";
const client = createTrackedClient(OpenAI, {
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com",
owlmetricToken: process.env.OWLMETRIC_DEEPSEEK_TOKEN,
provider: "DeepSeek",
});
DeepSeek R1 provides detailed reasoning:
const response = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "user", content: "Explain your reasoning process when solving: What comes next in the sequence 2, 4, 8, 16, ?" }
],
max_tokens: 500,
temperature: 0.1,
});
// Reasoning tokens tracked separately for transparency
DeepSeek Coder excels at code analysis:
const response = await client.chat.completions.create({
model: "deepseek-coder",
messages: [
{ role: "user", content: "Analyze this code for potential bugs and improvements:\n\n```python\ndef factorial(n):\n if n == 0:\n return 1\n return n * factorial(n-1)\n```" }
],
max_tokens: 400,
});
// Code analysis tokens optimized for DeepSeek pricing
const response = await client.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "user", content: "Prove that the square root of 2 is irrational. Show all steps." }
],
max_tokens: 800,
temperature: 0.0, // Deterministic for mathematical proofs
});
// Mathematical reasoning tracked with detailed token breakdown
// Optimize for cache hits with consistent context
const systemPrompt = "You are an expert programmer specializing in Python.";
const responses = await Promise.all([
client.chat.completions.create({
model: "deepseek-coder",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: "Write a function to sort a list." }
]
}),
client.chat.completions.create({
model: "deepseek-coder",
messages: [
{ role: "system", content: systemPrompt }, // Same system prompt = cache hit
{ role: "user", content: "Write a function to reverse a string." }
]
})
]);
// Second request benefits from cached system prompt