RobGruhl/hello-perplexity

โ˜… 0Forks 0JavaScriptGitHub โ†—Compare

README

hello-perplexity

Canonical reference for the Perplexity API. Clean client, runnable examples, docs.

Perplexity is a search engine, not an LLM. You have Claude for reasoning โ€” Perplexity is for current web information with citations.

๐Ÿ”Ž House rule: Search API + Agent API (Claude), never Sonar

Two tools, both grounded in Perplexity search:

  • search() โ€” raw ranked web results + citations, flat-rate ($5/1K), no LLM. Hand results to Claude (me) to reason over.
  • agent() โ€” a third-party frontier model (default Claude Sonnet 4.6) with live web search + citations. This is the "Claude, with search" path. Endpoint POST /v1/agent; priced at the provider's direct rate ($3/M in, $15/M out) + ~$0.005/search.

The Perplexity Sonar models are intentionally NOT used โ€” no chat() / reason() / deepResearch() (sonar / sonar-pro / sonar-reasoning-pro / sonar-deep-research). Those stay in the lib for API-reference completeness and downstream copies, but they're not the path here. If you want a synthesized answer, use agent() (Claude); if you want raw sources, use search().

Quick Start

cp .env.example .env      # add your pplx-... key
bun install               # installs dotenv (examples use `import 'dotenv/config'`)
node examples/01-basic-search.js

Ad-hoc scripts must load the key themselves โ€” either import 'dotenv/config'; at the top (the examples' pattern) or run with node --env-file=.env script.mjs. The lib does not auto-load .env.

The Search API (/search)

Flat-rate web search ($5/1K requests). No model. Returns raw results (title, url, snippet) + citations. Supports batch queries (array of up to 5 strings), domain filtering, and date ranges.

import 'dotenv/config';
import { search } from './lib/perplexity.js';

const { results, citations } = await search('your query');
results.forEach(r => console.log(r.title, r.url, r.snippet));

// Explicit API key (vs env var)
import { createClient } from './lib/perplexity.js';
const pplx = createClient('pplx-...');
await pplx.search('query');

There is no synthesized answer field from /search โ€” that's by design. Hand the results + citations to Claude for synthesis.

The Agent API (/v1/agent) โ€” Claude Sonnet with search

A third-party frontier model with Perplexity web-search grounding. Default model is anthropic/claude-sonnet-4-6. OpenAI /v1/responses-compatible. Token-priced at the provider's direct rate (no markup) + a per-search tool charge.

import 'dotenv/config';
import { agent } from './lib/perplexity.js';

const { answer, citations, results, usage } = await agent('your question', {
  // model: 'anthropic/claude-sonnet-4-6',   // default; also CLAUDE_OPUS / CLAUDE_HAIKU in AGENT_MODELS
  // webSearch: true,                         // default; set false to disable the search tool
  instructions: 'Use web_search for anything recent. Cite sources.',
});
console.log(answer);            // Claude's synthesized answer
console.log(citations);         // source URLs
console.log(usage.cost.total_cost);  // $ for the call

Model IDs live in AGENT_MODELS (constants). Verified live: a Sonnet call with one web search โ‰ˆ $0.015โ€“0.02.

Examples

# File What
01 basic-search.js Simplest Search API call
02 domain-filtering.js Allow/block domains
03 date-filtering.js Recency + date range + spoiler safety
10 parallel-search.js Concurrent search queries
11 agent-claude.js Agent API โ€” Claude Sonnet 4.6 + web search

Search + Agent examples are the ones used here. (04 chat-completions, 05 deep-research, 06 reasoning, 07 streaming, 08 structured-output, 09 openai-sdk-compat remain on disk as Sonar/Chat-API references but are not used here โ€” see House rule above.)

Docs

  • API Reference โ€” Both endpoints, auth, request/response shapes
  • Models โ€” Model comparison (Chat API reference only)
  • Parameters โ€” Every parameter with examples and gotchas
  • MCP Setup โ€” Using Perplexity as MCP tools for Claude
  • Patterns โ€” Domain filtering, spoiler safety, authority tiers, parallel search
  • Pricing โ€” Cost per endpoint/model, optimization strategies

Used In

Contributors

RobGruhl

Issues