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 both ValyuSearchTool for direct tool usage and ValyuRetriever for document retrieval workflows.
You’ll also need to set your API keys. Create a .env file in your project root:
Copy
# .envVALYU_API_KEY=your-valyu-api-key-hereANTHROPIC_API_KEY=your-anthropic-api-key-here # Optional for later examplesOPENAI_API_KEY=your-openai-api-key-here # Optional for later examples
The most powerful way to use Valyu is within LangChain agents, where the AI can dynamically decide when and how to search:
Copy
pip install langchain-anthropic
Copy
from langchain_valyu import ValyuSearchToolfrom langchain_anthropic import ChatAnthropicfrom langgraph.prebuilt import create_react_agentfrom datetime import datetimefrom dotenv import load_dotenv# Load environment variables from .env fileload_dotenv()# Initialize agent componentsllm = ChatAnthropic(model="claude-sonnet-4-20250514")valyu_search_tool = ValyuSearchTool()# Create agent with Valyu Deepsearchagent = create_react_agent( llm, [valyu_search_tool], prompt=f"Today is ${datetime.now().strftime('%Y-%m-%d')}. You are a helpful assistant that has access to a tool to search for information.",)# User messageinput_message = { "role": "user", "content": "What are the benchmarks of the new Grok 4 model from xAI, and how does it compare to the previous models?",}# Stream agent tool calls and responsefor step in agent.stream( {"messages": [input_message]}, stream_mode="values",): step["messages"][-1].pretty_print()
from langchain_valyu import ValyuSearchToolfrom langchain_anthropic import ChatAnthropicfrom langgraph.prebuilt import create_react_agentfrom langchain_core.messages import HumanMessage, SystemMessage# Create financial research agentfinancial_llm = ChatAnthropic(model="claude-sonnet-4-20250514")valyu_tool = ValyuSearchTool()financial_agent = create_react_agent(financial_llm, [valyu_tool])# Query financial markets with system contextquery = "What are the latest developments in cryptocurrency regulation and their impact on institutional adoption?"system_context = SystemMessage(content="""You are a financial research assistant. Use Valyu to search for:- Real-time market data and news- Academic research on financial models- Economic indicators and analysisAlways cite your sources and provide context about data recency.""")for step in financial_agent.stream( {"messages": [system_context, HumanMessage(content=query)]}, stream_mode="values",): step["messages"][-1].pretty_print()
# Set appropriate price limits based on use casetool = ValyuSearchTool()# For quick lookupsquick_search = tool._run( query="current bitcoin price", max_price=30.0, # Lower cost for simple queries max_num_results=3)# For comprehensive researchdetailed_search = tool._run( query="comprehensive analysis of renewable energy trends", max_price=50.0, # Higher budget for complex queries max_num_results=15, search_type="all")
from langchain_core.messages import SystemMessage, HumanMessage# Optimize agent behavior with good system messagessystem_message = SystemMessage(content="""You are an AI research assistant with access to Valyu search.SEARCH GUIDELINES:- Use search_type="proprietary" for academic/scientific queries- Use search_type="web" for current events and news- Use search_type="all" for comprehensive research- Set higher relevance_threshold (0.6+) for precise results- Use category parameter to guide search context- Always cite sources from search resultsRESPONSE FORMAT:- Provide direct answers based on search results- Include source citations with URLs when available- Mention publication dates for time-sensitive information- Indicate if information might be outdated""")agent = create_react_agent(llm, [ValyuSearchTool()])# Use the agent with system contextfor step in agent.stream( {"messages": [system_message, HumanMessage(content="Your query here")]}, stream_mode="values",): step["messages"][-1].pretty_print()