# <img src="https://models.dev/logos/perplexity.svg" alt="Perplexity logo" className="inline w-8 h-8 mr-2 align-middle dark:invert dark:brightness-0 dark:contrast-200" />Perplexity
[EN] Source: https://mastra.ai/models/providers/perplexity

Access 4 Perplexity models through Mastra's model router. Authentication is handled automatically using the `PERPLEXITY_API_KEY` environment variable.

Learn more in the [Perplexity documentation](https://docs.perplexity.ai).

```bash
PERPLEXITY_API_KEY=your-api-key
```

```typescript
import { Agent } from "@mastra/core";

const agent = new Agent({
  name: "my-agent",
  instructions: "You are a helpful assistant",
  model: "perplexity/sonar"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
  console.log(chunk);
}
```

:::info

Mastra uses the OpenAI-compatible `/chat/completions` endpoint. Some provider-specific features may not be available. Check the [Perplexity documentation](https://docs.perplexity.ai) for details.

:::

## Models

<ProviderModelsTable
  models={[
  {
    "model": "perplexity/sonar",
    "imageInput": false,
    "audioInput": false,
    "videoInput": false,
    "toolUsage": false,
    "reasoning": false,
    "contextWindow": 128000,
    "maxOutput": 4096,
    "inputCost": 1,
    "outputCost": 1
  },
  {
    "model": "perplexity/sonar-pro",
    "imageInput": true,
    "audioInput": false,
    "videoInput": false,
    "toolUsage": false,
    "reasoning": false,
    "contextWindow": 200000,
    "maxOutput": 8192,
    "inputCost": 3,
    "outputCost": 15
  },
  {
    "model": "perplexity/sonar-reasoning",
    "imageInput": false,
    "audioInput": false,
    "videoInput": false,
    "toolUsage": false,
    "reasoning": true,
    "contextWindow": 128000,
    "maxOutput": 4096,
    "inputCost": 1,
    "outputCost": 5
  },
  {
    "model": "perplexity/sonar-reasoning-pro",
    "imageInput": true,
    "audioInput": false,
    "videoInput": false,
    "toolUsage": false,
    "reasoning": true,
    "contextWindow": 128000,
    "maxOutput": 4096,
    "inputCost": 2,
    "outputCost": 8
  }
]}
/>

## Advanced Configuration

### Custom Headers

```typescript
const agent = new Agent({
  name: "custom-agent",
  model: {
    url: "https://api.perplexity.ai",
    id: "perplexity/sonar",
    apiKey: process.env.PERPLEXITY_API_KEY,
    headers: {
      "X-Custom-Header": "value"
    }
  }
});
```

### Dynamic Model Selection

```typescript
const agent = new Agent({
  name: "dynamic-agent",
  model: ({ runtimeContext }) => {
    const useAdvanced = runtimeContext.task === "complex";
    return useAdvanced
      ? "perplexity/sonar-reasoning-pro"
      : "perplexity/sonar";
  }
});
```



## Direct Provider Installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the [package documentation](https://www.npmjs.com/package/@perplexity-ai/perplexity_ai) for more details.

```bash npm2yarn copy
npm install @perplexity-ai/perplexity_ai
```

For detailed provider-specific documentation, see the [AI SDK Perplexity provider docs](https://ai-sdk.dev/providers/ai-sdk-providers/perplexity).


---
title: "Requesty | Models | Mastra"
description: "Use Requesty models with Mastra. 13 models available."
---

{/* This file is auto-generated by generate-model-docs.ts - DO NOT EDIT MANUALLY */}