---
title: LangChain Web Search Agent
description: Build a Python or JavaScript LangChain agent that searches Gyaansetu and reads its ranked Results.
slug: langchain-agent
---

# LangChain Web Search Agent

Add a Gyaansetu API Key that starts with `gs-` and use the Search tool in a LangChain agent. Search returns ranked `results` with a title, URL and Content for each Result.

## Python

Install the agent, model provider and Search tool packages:

```bash
pip install -U langchain langchain-openai langchain-tavily
```

Point the tool at Gyaansetu, then pass it to the agent:

```python
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_tavily import TavilySearch

search = TavilySearch(
    tavily_api_key="gs-YOUR_GYAANSETU_API_KEY",
    api_base_url="https://web-search.gyaansetu.com",
    max_results=5,
)
agent = create_agent(model=ChatOpenAI(model="gpt-4.1-mini"), tools=[search])
response = agent.invoke({"messages": [{"role": "user", "content": "What is MCP?"}]})

# Inspect the tool message to see the Search response the agent received.
for message in response["messages"]:
    if message.type == "tool":
        print(message.content)

# A direct call returns a dictionary with Results you can read in application code.
result = search.invoke({"query": "What is MCP?"})
for item in result["results"]:
    print(item["title"], item["url"], item["content"])
```

## JavaScript

Install LangChain, its OpenAI integration and the Search tool:

```bash
npm install langchain @langchain/openai @langchain/tavily
```

The `apiBaseUrl` option directs the tool to Gyaansetu. Use a `gs-` API Key:

```javascript
import { createAgent } from "langchain";
import { TavilySearch } from "@langchain/tavily";

const search = new TavilySearch({
  tavilyApiKey: "gs-YOUR_GYAANSETU_API_KEY",
  apiBaseUrl: "https://web-search.gyaansetu.com",
  maxResults: 5,
});
const agent = createAgent({ model: "openai:gpt-4.1-mini", tools: [search] });
const response = await agent.invoke({
  messages: [{ role: "user", content: "What is MCP?" }],
});

// The tool message contains the Search response the agent received.
for (const message of response.messages) {
  if (message.getType() === "tool") console.log(message.content);
}

// Read the ranked Results directly when your application needs their fields.
const result = await search.invoke({ query: "What is MCP?" });
for (const item of result.results) {
  console.log(item.title, item.url, item.content);
}
```
