Skip to main content

Overview

LangChain is a framework for building LLM applications, and LangGraph adds stateful agents on top of it. Reach Perplexity’s Agent API through langchain-openai’s ChatOpenAI with use_responses_api=True, pointed at Perplexity’s base URL.
Use ChatOpenAI from langchain-openai — not ChatPerplexity from langchain-perplexity, which only speaks Sonar Chat Completions. Setting use_responses_api=True routes calls to POST /v1/responses (the Agent API).

Installation

API Key Setup

Get API Key

Generate your Perplexity API key from the API portal.

Chat Model

Point ChatOpenAI at Perplexity’s /v1 base URL and enable the built-in web_search tool for grounded answers:
Perplexity’s built-in web_search is best-in-class web-grounded search — the model calls it to ground answers in live sources. See the web_search tool docs for filters and options.
Pass Perplexity’s built-in tools (web_search, fetch_url) through model_kwargs={"tools": [...]}, and Agent-API-only fields such as preset through extra_body — the underlying OpenAI SDK rejects them as unknown top-level keyword arguments. preset="medium" selects a deep-research configuration; see Presets for faster, lighter options like fast and low.

LangGraph Agent

Drop the grounded model into a LangGraph agent. Because web_search runs server-side, no client-side tools are required:
web_search gives the agent Perplexity’s best-in-class web-grounded search, so it can pull in and reason over live sources when a query needs them. Learn more in the web_search tool docs.

Reading Sources

To read structured search_results (titles and URLs), call the Agent API directly with the Perplexity SDK:

Agent API Quickstart

Build with Agent API models, tools, and presets.

Agent API Models

Available models and pricing.

LangGraph Docs

Build stateful agents with LangGraph.