Load testing and capacity planning for Perplexity Sonar API. Key constraint: Perplexity rate limits at 50 RPM (default tier), and every request performs a live web search with variable latency. Load testing must respect these limits to avoid burning through credits.
Capacity Constraints
Constraint
Default Limit
Impact
RPM (requests per minute)
50
Hard ceiling on throughput
Context window
127K tokens
Limits conversation history
sonar latency
1-3s
Throughput: ~20-50 concurrent
sonar-pro latency
3-8s
Throughput: ~6-16 concurrent
search_domain_filter
20 domains max
Per-request limit
Prerequisites
k6 load testing tool installed
Separate Perplexity API key for load testing
Budget approval (load tests cost money)
Instructions
Step 1: k6 Load Test Script
// perplexity-load-test.js
import http from "k6/http";
import { check, sleep } from "k6";
import { Rate, Trend } from "k6/metrics";
const errorRate = new Rate("perplexity_errors");
const citationCount = new Trend("perplexity_citations");
export const options = {
stages: [
{ duration: "1m", target: 5 }, // Ramp to 5 VUs
{ duration: "3m", target: 5 }, // Steady at 5 VUs
{ duration: "1m", target: 15 }, // Ramp to 15 VUs
{ duration: "3m", target: 15 }, // Steady at 15 VUs
{ duration: "1m", target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ["p(95)<10000"], // 10s P95 for sonar
perplexity_errors: ["rate<0.05"], // <5% error rate
},
};
const queries = [
"What is TypeScript?",
"Latest Node.js features",
"Python vs JavaScript for web development",
"Current state of AI in healthcare",
"Best practices for REST API design",
];
export default function () {
const query = queries[Math.floor(Math.random() * queries.length)];
const response = http.post(
"https://api.perplexity.ai/chat/completions",
JSON.stringify({
model: "sonar",
messages: [{ role: "user", content: query }],
max_tokens: 200,
}),
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${__ENV.PERPLEXITY_API_KEY}`,
},
timeout: "15s",
}
);
const success = check(response, {
"status is 200": (r) => r.status === 200,
"has content": (r) => {
try { return JSON.parse(r.body).choices[0].message.content.length > 0; }
catch { return false; }
},
});
errorRate.add(!success);
if (response.status === 200) {
try {
const body = JSON.parse(response.body);
citationCount.add(body.citations?.length || 0);
} catch {}
}
// Critical: stay within 50 RPM
sleep(1.5 + Math.random());
}
Step 2: Run Load Test
set -euo pipefail
# Minimal test (5 queries, verify setup)
k6 run --vus 1 --duration 30s \
--env PERPLEXITY_API_KEY=$PERPLEXITY_API_KEY \
perplexity-load-test.js
# Full test (respecting 50 RPM)
k6 run --env PERPLEXITY_API_KEY=$PERPLEXITY_API_KEY \
perplexity-load-test.js