Learning resourcesReviewed · Perplexity

Use Search API with the Anthropic SDK

This tutorial teaches Python and TypeScript developers to add Perplexity web search to Claude, with a manual tool loop they must implement. It covers tool definitions, search-result formatting, message handling and streaming, and requires API credentials for both services.

Researched 25 Nov 2026

In short.

This tutorial helps Python and TypeScript developers connect Claude to Perplexity web search, with their application responsible for executing searches and returning results to the model. It defines a web_search tool, limits each search request to three queries and five combined results, and explains message pairing and streaming. The examples provide a concrete integration pattern, while pricing, current model compatibility and answer quality remain unknown. No code was executed.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage includes the complete documentation page at [https://docs.perplexity.ai/docs/search/agent-sdks/anthropic.md], including its Python and TypeScript examples, streaming guidance, operational notes and link descriptions. Linked documentation, package contents, pricing, service terms and external notices are outside this scope. No license notice appears in the supplied page. No code or API requests were executed, and runtime compatibility, performance and answer quality were not assessed.

The overview

Identity

Use Search API with the Anthropic SDK is a developer tutorial hosted in Perplexity's documentation at [https://docs.perplexity.ai/docs/search/agent-sdks/anthropic.md]. It explains how to expose Perplexity Search API as a tool through Anthropic's Messages API.

The resource contains Python and TypeScript examples. Its central pattern is an application-managed conversation loop in which Claude requests searches, the application executes them, and Claude receives the results before continuing.

Assessment

The guide is useful for developers who want concrete control over how search results reach Claude. It shows the connection between the tool schema, search handler and conversation history, making the responsibilities of each component visible.

Its examples are a starting point for implementation. They do not show an explicit limit on loop iterations, comprehensive exception handling or measured results. Reliability, compatibility and answer quality have not been assessed through execution.

Users

The tutorial suits developers building Claude applications that need current or externally sourced information. Beginners need enough Python or TypeScript knowledge to install packages, configure environment variables and follow API request and response objects.

Experienced developers can use its specific guidance on multiple tool calls, result identifiers, streaming and retrieval filters. Anyone seeking a ready-to-use search application would still need to build the surrounding application.

Capabilities

The examples let Claude request web searches and receive titles, URLs and snippets. The handler sends up to three queries in one Search API request and sets max_results to five across the combined results. The page states that the underlying API accepts up to five queries; three is this integration's chosen limit.

The handler removes results with empty snippets, deduplicates results by URL, replaces snippet line breaks with spaces and truncates snippets after 400 characters, adding an ellipsis. The guide also explains streaming and identifies where developers can add domain, country and date constraints.

Using it

Requirements

The examples require access to Anthropic's Messages API and Perplexity's Search API, with ANTHROPIC_API_KEY and PERPLEXITY_API_KEY configured as environment variables. Python uses the anthropic and perplexityai packages; TypeScript uses @anthropic-ai/sdk and @perplexity-ai/perplexity_ai.

Minimum language-runtime versions, dependency versions and deployment requirements are not specified. The examples use the model identifier claude-sonnet-5-5, but its availability and compatibility are not independently established by the page.

Mechanism

A tool named web_search exposes a queries array through an input_schema requiring one to three strings. Claude can respond with a tool_use block instead of a final answer. The application then passes the requested queries to perplexity.search.create and formats the returned results as text.

When stop_reason is tool_use, the application saves the assistant response, executes every requested tool call and creates matching tool_result blocks using each call's tool_use_id. All results from that turn go into one subsequent user message. The loop continues until stop_reason differs from tool_use, when the example returns the response's text blocks.

For streaming, the SDK accumulates partial tool arguments and exposes the completed message through get_final_message() in Python or finalMessage() in TypeScript. The application then resumes the same tool-handling process.

Setup

The documented Python installation command is pip install anthropic perplexityai. The TypeScript command is npm install @anthropic-ai/sdk @perplexity-ai/perplexity_ai. Configure both API-key environment variables, then assemble the tool-description strings, tool definition, search handler and conversation loop in the chosen language.

The TypeScript tool-definition excerpt includes placeholder declarations for strings defined earlier; the page says to remove those declarations when combining the steps into one file. Before using the example, confirm the model identifier and SDK compatibility for the intended environment.

Retrieval restrictions belong in the search handler. A date written into a query can influence relevance, but restricting publication dates requires an explicit recency or date filter.

Example

The supplied example asks, "What were the major AI infrastructure announcements this week?" It passes that question into chat_with_search in Python or chatWithSearch in TypeScript. If Claude requests web_search, the application retrieves snippets and returns them through tool_result messages so Claude can continue.

The handler includes source URLs beside snippets, which the guide says helps Claude cite sources. The page supplies no resulting searches, citations or final answer, so this example establishes the intended interaction rather than a demonstrated outcome.

Assessment & evidence

Limitations

The guide recommends short keyword queries and separate queries for separate entities. It describes complex queries requiring multi-step reasoning as unsupported by the search tool. The surrounding Claude conversation can request further searches, but the page provides no measured evidence of how effectively it does so.

Claude receives shortened snippets, and the handler can return fewer than five results after removing empty snippets and duplicate URLs. Including source URLs supports citation generation, but citation accuracy and factual completeness remain unassessed.

The main examples do not implement explicit search-exception recovery, retries or an iteration ceiling. Error signaling through is_error is explained separately. Multiple tool calls may be requested in one turn, while the shown loops execute them sequentially.

The final-answer branch returns text whenever stop_reason differs from tool_use. It does not separately handle a response stopped by the output-token limit.

Reasoning

The integration separates retrieval from answer generation: Perplexity supplies search results, Claude interprets them, and application code controls execution and message history. That separation gives developers a clear place to apply fixed search constraints and result formatting.

The query and result caps bound each individual search response, while URL deduplication and snippet truncation reduce the text sent back to Claude. These are visible implementation choices; improvements in accuracy, latency or cost are not established by comparative measurements.

Alternatives

The page links to companion Search API integration guides for the OpenAI SDK at [https://docs.perplexity.ai/docs/search/agent-sdks/openai.md] and the Gemini SDK at [https://docs.perplexity.ai/docs/search/agent-sdks/gemini.md]. These are relevant starting points for developers using those SDKs.

The linked pages are outside the available evidence. Their implementations, requirements, costs and comparative benefits are not assessed, and this page provides no comparison with other search services.

Evidence

The evidence is the complete documentation page at [https://docs.perplexity.ai/docs/search/agent-sdks/anthropic.md]. It contains installation instructions, tool descriptions and schemas, handlers, conversation loops, streaming excerpts and operational notes in Python and TypeScript.

The page provides implementation detail without execution logs, sample outputs, benchmarks or a compatibility matrix. Actual runtime behavior, performance and search quality remain unknown.

Access & freshness

Costs

Pricing for Anthropic API calls and Perplexity searches is absent. Free allowances, subscription requirements, rate-limit tiers and total operating cost are unknown from this page.

One question can trigger multiple model turns and search requests. Each shown Messages API request sets max_tokens to 1024, but the loop has no explicit total-turn or spending limit. The five-result cap limits returned search results; it does not establish a price or a total request budget.

Rights

The supplied page states no license for its prose or code examples. Permission to redistribute those materials is therefore unknown from this evidence.

The page also does not establish rights to reuse retrieved snippets, API contractual terms or data-retention commitments. Returning a URL with a snippet does not establish permission to republish the underlying content.

Freshness

The available page snapshot was captured at 2026-11-25T19:23:49.445Z. The page itself supplies no publication date, last-update date or revision history, so the capture time does not establish when its examples were written.

Dependencies are unpinned, and the sample model identifier is claude-sonnet-5-5. Present availability of that model and compatibility with currently installed SDK versions remain unverified.

Actions

Before adopting the example, confirm access to both APIs, the intended model identifier, compatible SDK versions and current service pricing. Choose Python or TypeScript and assemble the corresponding excerpts into one implementation.

For an operational application, add explicit failure handling and a total loop budget. Keep every tool call paired with its result, and add date or domain filters in the handler when the task needs those restrictions.

Evaluate the implementation with questions whose answers and sources can be checked. Assess citation accuracy, time filtering, empty results and API failures before relying on it for consequential work. These are recommended next steps; no execution or testing was performed.

Evidence for this report

selected-page.txtselected · checked 2026-11-25