Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(client): attribute Perplexity requests (#1628)

Saleh Alghusson5c6b95d2d9b9
diff --git a/src/client/src/__tests__/lib/platform/chat/stream.test.ts b/src/client/src/__tests__/lib/platform/chat/stream.test.ts
index 790f21c8..b6574639 100644
--- a/src/client/src/__tests__/lib/platform/chat/stream.test.ts
+++ b/src/client/src/__tests__/lib/platform/chat/stream.test.ts
@@ -101,6 +101,15 @@ describe('getModelInstance', () => {
expect(instance).toBeDefined();
});
+ it('attributes Perplexity requests to OpenLIT', () => {
+ getModelInstance('perplexity', 'key', 'sonar');
+ expect(createOpenAI).toHaveBeenCalledWith({
+ baseURL: 'https://api.perplexity.ai',
+ apiKey: 'key',
+ headers: { 'X-Pplx-Integration': 'openlit' },
+ });
+ });
+
it('supports all built-in providers including MiniMax', () => {
const providers = [
'openai', 'anthropic', 'google', 'mistral', 'cohere',
diff --git a/src/client/src/__tests__/lib/platform/evaluation/run-evaluation.test.ts b/src/client/src/__tests__/lib/platform/evaluation/run-evaluation.test.ts
index 328d29ad..f5a8c4c8 100644
--- a/src/client/src/__tests__/lib/platform/evaluation/run-evaluation.test.ts
+++ b/src/client/src/__tests__/lib/platform/evaluation/run-evaluation.test.ts
@@ -96,7 +96,11 @@ describe('runEvaluation — provider routing', () => {
it('creates OpenAI-compatible model for provider=perplexity', async () => {
await runEvaluation({ ...BASE_PARAMS, provider: 'perplexity' });
- expect(createOpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: 'https://api.perplexity.ai' }));
+ expect(createOpenAI).toHaveBeenCalledWith({
+ baseURL: 'https://api.perplexity.ai',
+ apiKey: 'sk-test',
+ headers: { 'X-Pplx-Integration': 'openlit' },
+ });
});
it('creates OpenAI-compatible model for provider=deepseek', async () => {
diff --git a/src/client/src/__tests__/lib/platform/openground/ai-sdk-adapter.test.ts b/src/client/src/__tests__/lib/platform/openground/ai-sdk-adapter.test.ts
new file mode 100644
index 00000000..0c7c7e76
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/openground/ai-sdk-adapter.test.ts
@@ -0,0 +1,32 @@
+jest.mock('ai', () => ({ generateText: jest.fn() }));
+jest.mock('@ai-sdk/openai', () => ({ createOpenAI: jest.fn() }));
+jest.mock('@ai-sdk/anthropic', () => ({ createAnthropic: jest.fn() }));
+jest.mock('@ai-sdk/google', () => ({ google: jest.fn() }));
+jest.mock('@ai-sdk/mistral', () => ({ createMistral: jest.fn() }));
+jest.mock('@ai-sdk/cohere', () => ({ createCohere: jest.fn() }));
+
+import { generateText } from 'ai';
+import { createOpenAI } from '@ai-sdk/openai';
+import { AISdkAdapter } from '@/lib/platform/openground/ai-sdk-adapter';
+
+it('attributes Perplexity requests to OpenLIT', async () => {
+ const model = {};
+ (createOpenAI as jest.Mock).mockReturnValue(() => model);
+ (generateText as jest.Mock).mockResolvedValue({
+ text: 'response',
+ usage: { inputTokens: 1, outputTokens: 1 },
+ finishReason: 'stop',
+ });
+
+ await AISdkAdapter.generateCompletion({
+ provider: 'perplexity',
+ model: 'sonar',
+ apiKey: 'key',
+ });
+
+ expect(createOpenAI).toHaveBeenCalledWith({
+ baseURL: 'https://api.perplexity.ai',
+ apiKey: 'key',
+ headers: { 'X-Pplx-Integration': 'openlit' },
+ });
+});
diff --git a/src/client/src/lib/platform/chat/stream.ts b/src/client/src/lib/platform/chat/stream.ts
index f3c49687..ddb4a3b3 100644
--- a/src/client/src/lib/platform/chat/stream.ts
+++ b/src/client/src/lib/platform/chat/stream.ts
@@ -27,7 +27,11 @@ const providerFactories: Record<string, ProviderFactory> = {
mistral: (apiKey) => createMistral({ apiKey }),
cohere: (apiKey) => createCohere({ apiKey }),
groq: (apiKey) => createOpenAI({ baseURL: "https://api.groq.com/openai/v1", apiKey }),
- perplexity: (apiKey) => createOpenAI({ baseURL: "https://api.perplexity.ai", apiKey }),
+ perplexity: (apiKey) => createOpenAI({
+ baseURL: "https://api.perplexity.ai",
+ apiKey,
+ headers: { "X-Pplx-Integration": "openlit" },
+ }),
azure: (apiKey) => createOpenAI({
baseURL: process.env.AZURE_OPENAI_ENDPOINT || "https://your-resource.openai.azure.com",
apiKey,
diff --git a/src/client/src/lib/platform/evaluation/run-evaluation.ts b/src/client/src/lib/platform/evaluation/run-evaluation.ts
index 04e573f0..abac1788 100644
--- a/src/client/src/lib/platform/evaluation/run-evaluation.ts
+++ b/src/client/src/lib/platform/evaluation/run-evaluation.ts
@@ -87,6 +87,7 @@ function getModel(provider: string, model: string, apiKey: string) {
return createOpenAI({
baseURL: "https://api.perplexity.ai",
apiKey,
+ headers: { "X-Pplx-Integration": "openlit" },
})(model);
case "deepseek":
return createOpenAI({
diff --git a/src/client/src/lib/platform/openground/ai-sdk-adapter.ts b/src/client/src/lib/platform/openground/ai-sdk-adapter.ts
index afd9a52a..c627b937 100644
--- a/src/client/src/lib/platform/openground/ai-sdk-adapter.ts
+++ b/src/client/src/lib/platform/openground/ai-sdk-adapter.ts
@@ -41,7 +41,8 @@ export class AISdkAdapter {
}),
perplexity: (apiKey: string) => createOpenAI({
baseURL: 'https://api.perplexity.ai',
- apiKey
+ apiKey,
+ headers: { 'X-Pplx-Integration': 'openlit' },
}),
azure: (apiKey: string) => createOpenAI({
baseURL: process.env.AZURE_OPENAI_ENDPOINT || 'https://your-resource.openai.azure.com',