Overview
LangChain is a framework for building LLM applications, and LangGraph adds stateful agents on top of it. Reach Perplexity’s Agent API throughlangchain-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
PointChatOpenAI at Perplexity’s /v1 base URL and enable the built-in web_search tool for grounded answers:
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 withcreate_agent from LangChain. Pass Perplexity’s built-in tools in the agent’s tools list:
Reading Sources
To read structuredsearch_results (titles and URLs), call the Agent API directly with the Perplexity SDK:
Links & Resources
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.