Perplexity with Claude Code
Overview
Section titled “Overview”Claude Code is Anthropic's agentic coding tool that lives in your terminal, IDE, and CI. It can edit files, run commands, and call Model Context Protocol (MCP) servers as part of its workflow. This guide covers three integration paths for Perplexity:
Official TypeScript SDK
Let Claude Code write and run application code that calls the Perplexity API directly. Recommended.
OpenAI-Compatible SDK
Reuse an existing OpenAI client by pointing baseURL at https://api.perplexity.ai/v1.
Claude Code MCP
Register the hosted Perplexity Docs MCP with claude mcp add so Claude Code can look up Perplexity docs while you work.
Prerequisites
Section titled “Prerequisites”- Claude Code installed — see the Claude Code quickstart
- Node.js 18+ for the TypeScript examples
- A Perplexity API key
Get API Key
Generate a key from the Perplexity API Portal.
Get Key
API key handling
Section titled “API key handling”Never hardcode your API key in source files or commit it to a repository. Store it in an environment variable and read it at runtime:
export PERPLEXITY_API_KEY="your_api_key_here"setx PERPLEXITY_API_KEY "your_api_key_here"Create a .env file in your project root and add it to .gitignore:
# .env
PERPLEXITY_API_KEY=your_api_key_hereLoad it at startup (for example with dotenv):
import "dotenv/config";Path 1: Official TypeScript SDK (recommended)
Section titled “Path 1: Official TypeScript SDK (recommended)”Have Claude Code scaffold and edit code that calls the Perplexity API using the official SDK. This is the most reliable path and gives you full type safety, preset support, and access to every API feature.
Install
Section titled “Install”npm install @perplexity-ai/perplexity_aiAgent API with low
Section titled “Agent API with low”The Agent API is the recommended surface for most applications. Use the low preset for web-grounded responses with sensible defaults:
import Perplexity from "@perplexity-ai/perplexity_ai";
const client = new Perplexity(); // reads PERPLEXITY_API_KEY from the environment
const response = await client.responses.create({
preset: "low",
input: "Summarize the latest changes to the Perplexity Agent API.",
});
console.log(`Model used: ${response.model}`);
console.log(response.output_text);Python equivalent
from perplexity import Perplexity
client = Perplexity() # reads PERPLEXITY_API_KEY from the environment
response = client.responses.create(
preset="low",
input="Summarize the latest changes to the Perplexity Agent API.",
)
print(f"Model used: {response.model}")
print(response.output_text)Add web search
Section titled “Add web search”Enable the web_search tool for explicit control over when search is used:
import Perplexity from "@perplexity-ai/perplexity_ai";
const client = new Perplexity();
const response = await client.responses.create({
model: "openai/gpt-5.6-terra",
input: "What are the latest developments in AI inference hardware?",
tools: [{ type: "web_search" }],
instructions:
"You have access to a web_search tool. Use it for questions about current events, news, or recent developments.",
});
if (response.status === "completed") {
console.log(response.output_text);
}For more presets, tools, and configuration options see the Agent API quickstart and the SDK overview.
Path 2: OpenAI-compatible SDK
Section titled “Path 2: OpenAI-compatible SDK”If your project already uses an OpenAI client, you can reuse it by pointing the baseURL at https://api.perplexity.ai/v1. Perplexity accepts POST /v1/responses as an alias for the Agent API.
Install
Section titled “Install”npm install openaiUse it
Section titled “Use it”import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.PERPLEXITY_API_KEY,
baseURL: "https://api.perplexity.ai/v1",
});
const response = await client.responses.create({
model: "openai/gpt-5.6-terra",
input: "Explain the key differences between REST and GraphQL APIs.",
});
console.log(response.output_text);Python equivalent
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("PERPLEXITY_API_KEY"),
base_url="https://api.perplexity.ai/v1",
)
response = client.responses.create(
model="openai/gpt-5.6-terra",
input="Explain the key differences between REST and GraphQL APIs.",
)
print(response.output_text)Path 3: Claude Code MCP for docs lookup
Section titled “Path 3: Claude Code MCP for docs lookup”Claude Code supports the Model Context Protocol (MCP). The hosted Perplexity Docs MCP at https://docs.perplexity.ai/mcp lets Claude Code search and read Perplexity's documentation directly — useful for grounding code suggestions in canonical docs without context-switching to a browser.
Add the server
Section titled “Add the server”Claude Code can register a remote MCP server over HTTP using the claude mcp add command with the --transport http flag:
Add the server only for the current project (writes to .mcp.json in your repo root):
claude mcp add --transport http --scope project perplexity-docs https://docs.perplexity.ai/mcpAdd the server for all projects on this machine:
claude mcp add --transport http --scope user perplexity-docs https://docs.perplexity.ai/mcpVerify and use
Section titled “Verify and use”Confirm the server is registered
Run
claude mcp listand check thatperplexity-docsappears in the output.Restart Claude Code
Quit and relaunch Claude Code (or run
/mcpfrom inside a session) so the new server is loaded.Use it from a prompt
Ask Claude Code something like "Using the Perplexity docs MCP, show me how to enable
lowin the Agent API." Claude Code will call the MCP server and ground its answer in current documentation.
Replacing Claude Code's model (not recommended)
Section titled “Replacing Claude Code's model (not recommended)”Claude Code is designed to run on Anthropic's Claude models, and the agent's tool use, prompting, and editing behavior are tuned to those models. Pointing the Claude Code CLI at the Perplexity API as an alternative model provider is not an officially supported configuration.
Instead:
- Use Perplexity from your project code (Path 1 or Path 2). Claude Code will happily write, edit, and run code that calls Perplexity for web-grounded answers, research, and search.
- Add the Perplexity Docs MCP (Path 3) so Claude Code can look up Perplexity documentation in-session.
This combination gives you Claude Code's editing and orchestration strengths alongside Perplexity's search-grounded responses, without depending on undocumented model-override behavior.
Next steps
Section titled “Next steps”Agent API quickstart
Presets, tools, and the full Agent API surface used by the examples above.
Perplexity SDK overview
Install, configure, and use the official Python and TypeScript SDKs.
OpenAI compatibility
Drop-in compatibility details, supported parameters, and migration tips.
Perplexity MCP Server
Add Perplexity search, ask, research, and reason tools to Claude Code and other MCP clients.