# LangChain Integration

> Use HushSearch as a LangChain tool for web search and extraction.

Source: http://localhost:3000/docs/guides/langchain

Integrate HushSearch into your LangChain agents and chains.

## Install

```bash
pip install langchain langchain-anthropic requests
```

## Custom LangChain Tool

```python
from langchain.tools import tool
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.prompts import ChatPromptTemplate

API_KEY = "hs_live_..."

@tool
def hushsearch_search(query: str) -> str:
    """Search the live web and return structured results."""
    r = requests.post(
        "https://api.hushsearch.io/v1/search/",
        headers={"X-API-Key": API_KEY},
        json={"query": query, "num_results": 5}
    )
    results = r.json()["results"]
    return "\n\n".join(
        f"**{r['title']}**\n{r['url']}\n{r['snippet'] or ''}"
        for r in results
    )

@tool
def hushsearch_extract(url: str) -> str:
    """Extract clean markdown content from a web page URL."""
    r = requests.post(
        "https://api.hushsearch.io/v1/extract/",
        headers={"X-API-Key": API_KEY},
        json={"url": url}
    )
    data = r.json()
    return f"# {data['title']}\n\n{data['markdown_content'][:3000]}"

llm = ChatAnthropic(model="claude-sonnet-4-6")
tools = [hushsearch_search, hushsearch_extract]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful research assistant with web access."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = executor.invoke({"input": "What's the latest news on AI agents?"})
print(result["output"])
```
