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

langchain 1.0 or later provides create_agent and installs LangGraph automatically.

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.
For a standalone chat model, pass Perplexity’s built-in tools (web_search, fetch_url) through model_kwargs={"tools": [...]}. Pass 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 the medium tier; see Presets for the full list.

LangChain Agent

Create an agent with create_agent from LangChain. Pass Perplexity’s built-in tools in the agent’s tools list:
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.
For agents, do not put built-in tools in model_kwargs. When LangChain binds the agent’s tools to the model, they replace model_kwargs["tools"]. Put built-in tool dictionaries and local tools in the same tools list so web search is not silently dropped: tools=[{"type": "web_search"}, my_local_tool].

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.