| name | perplexity-reliability-patterns |
| description | Implement reliability patterns for Perplexity Sonar API: circuit breaker, model fallback,
streaming timeout, and citation validation.
Trigger with phrases like "perplexity reliability", "perplexity circuit breaker",
"perplexity fallback", "perplexity resilience", "perplexity timeout".
|
| allowed-tools | Read, Write, Edit |
| version | 1.12.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","perplexity","perplexity-reliability"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Perplexity Reliability Patterns
Overview
Production reliability patterns for Perplexity Sonar API. Perplexity performs live web searches per request, making response times inherently variable. The key reliability challenges: search can stall, citations can break, and model tiers have different availability.
Prerequisites
- Perplexity API key configured
- Cache layer (Redis or in-memory)
- Understanding of search latency variability
Instructions
Step 1: Model Tier Fallback
import OpenAI from "openai";
const perplexity = new OpenAI({
apiKey: process.env.PERPLEXITY_API_KEY!,
baseURL: "https://api.perplexity.ai",
});
async function resilientSearch(
query: string,
preferredModel: string = "sonar-pro"
) {
const fallbackChain = [preferredModel, "sonar"];
let lastError: Error | null = null;
for (const model of fallbackChain) {
try {
const response = await perplexity.chat.completions.create({
model,
messages: [{ role: "user", content: query }],
: model === ? : ,
});
(model !== preferredModel) {
.();
}
{
: response.[].. || ,
: (response ). || [],
: response.,
: model !== preferredModel,
};
} (: ) {
lastError = err;
(err. === || err. === ) err;
.();
}
}
lastError || ();
}