Skip to main content

Overview

Best for: building Valyu into your product / agents. Use Valyu as a search and content tool inside LangChain (Python) agents.
Viewing the Python integration. For the TypeScript version of this guide, see the TypeScript LangChain integration.
Valyu integrates seamlessly with LangChain as a search tool, allowing you to enhance your AI agents and RAG applications with real-time web search and proprietary data sources. The integration provides LLM-ready context from multiple sources including web pages, academic journals, financial data, and more. The package includes two main tools:
  • ValyuSearchTool: Search operations with comprehensive parameter control
  • ValyuContentsTool: Extract clean content from specific URLs

Paste into your AI assistant to wire Valyu into a LangChain (Python) agent.

Open in Cursor

Installation

Install the official LangChain Valyu package:
pip install -U langchain-valyu
Configure credentials by setting the following environment variable:
export VALYU_API_KEY="your-valyu-api-key-here"
Or set it programmatically:
import os
os.environ["VALYU_API_KEY"] = "your-valyu-api-key-here"
For agent examples, you’ll also need:
export ANTHROPIC_API_KEY="your-anthropic-api-key"  # For Claude examples
export OPENAI_API_KEY="your-openai-api-key"        # For OpenAI examples

Free Credits

Get your API key with $10 free credits ($20 with a work email) from the Valyu Platform.

Basic Usage

import os
from langchain_valyu import ValyuSearchTool

# Set your API key
os.environ["VALYU_API_KEY"] = "your-api-key-here"

# Initialize the search tool
tool = ValyuSearchTool()

# Perform a search
search_results = tool._run(
    query="What are agentic search-enhanced large reasoning models?",
    search_type="all",  # "all", "web", or "proprietary"
    max_num_results=5,
    relevance_threshold=0.5,
    max_price=30.0
)

print("Search Results:", search_results.results)

Using ValyuContentsTool for Content Extraction

Extract clean, structured content from specific URLs:
import os
from langchain_valyu import ValyuContentsTool

# Set your API key
os.environ["VALYU_API_KEY"] = "your-api-key-here"

# Initialize the contents tool
contents_tool = ValyuContentsTool()

# Extract content from URLs
urls = [
    "https://arxiv.org/abs/2301.00001",
    "https://example.com/article",
]

extracted_content = contents_tool._run(urls=urls)
print("Extracted Content:", extracted_content.results)

# Print individual results
for result in extracted_content.results:
    print(f"URL: {result['url']}")
    print(f"Title: {result['title']}")
    print(f"Content: {result['content'][:200]}...")
    print(f"Status: {result['status']}")
    print("---")

Using with LangChain Agents

The most powerful way to use Valyu is within LangChain agents, where the AI can dynamically decide when and how to search:
pip install langchain-anthropic langgraph
import os
from langchain_valyu import ValyuSearchTool
from langchain_anthropic import ChatAnthropic
from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage

# Set API keys
os.environ["VALYU_API_KEY"] = "your-valyu-api-key"
os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-api-key"

# Initialize components
llm = ChatAnthropic(model="claude-sonnet-4-20250514")
valyu_search_tool = ValyuSearchTool()

# Create agent with Valyu search capability
agent = create_react_agent(llm, [valyu_search_tool])

# Use the agent
user_input = "What are the key factors driving recent stock market volatility, and how do macroeconomic indicators influence equity prices across different sectors?"

for step in agent.stream(
    {"messages": [HumanMessage(content=user_input)]},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()

Advanced configuration

results = tool._run(
    query="quantum computing breakthroughs 2024",
    max_num_results=10,  # 1-20 (up to 100 with a special API key)
    relevance_threshold=0.6,  # 0.0-1.0
    max_price=30.0,  # max cost per 1k retrievals (CPM)
    start_date="2024-01-01",  # YYYY-MM-DD
    end_date="2024-12-31",
    included_sources=["valyu/valyu-arxiv", "valyu/valyu-pubmed"],
    excluded_sources=["example.com"],
    response_length="medium",  # "short", "medium", "large", "max", or int
    country_code="US",  # 2-letter ISO code
    fast_mode=False,  # faster but shorter results
)
ValyuContentsTool takes a single required urls argument (max 10 per request).
Bind a SystemMessage to guide tool selection. Use search_type="all" for broad coverage (web + proprietary), or "web" for current events only; raise relevance_threshold (0.6+) for precise results; cite sources; and use natural-language queries, not operators.See the Prompting Guide for full guidelines.

Additional Resources

LangChain Valyu Tool

Official LangChain integration documentation

API Reference

Complete Valyu API documentation

LangGraph Agents

Build advanced agent workflows

Get API Key

Sign up for $10 free credits ($20 with a work email)