# LangChain

Load OrcaRouter tools as LangChain tools from the functions export and give them to any LangChain agent; each call runs the tool through OrcaRouter.

|  |  |
| --- | --- |
| Kind | framework |
| Connects through | functions |
| Docs | <https://docs.langchain.com/oss/python/langchain/tools> |

## Install

1. Install the packages: `pip install -U langchain langchain-openai requests`.
2. Set `ORCAROUTER_API_KEY` to an API key from your OrcaRouter console.
3. Load tools for a task with the helper below, then pass them to `create_agent`.

## Code

### Load OrcaRouter tools

```python
# orcarouter_tools.py
import os

import requests
from langchain_core.tools import StructuredTool

BASE = "https://api.orcarouter.ai"
HEADERS = {"Authorization": f"Bearer {os.environ['ORCAROUTER_API_KEY']}"}


def _runner(name):
    def run(**tool_input):
        r = requests.post(f"{BASE}/v1/tools/{name}/run", json={"input": tool_input}, headers=HEADERS, timeout=120)
        r.raise_for_status()
        return r.json()["data"]["output"]

    return run


def orcarouter_tools(query, limit=5):
    """Find catalog tools for a task and load them as LangChain tools."""
    found = requests.post(f"{BASE}/v1/tools/discover", json={"q": query, "limit": limit}, headers=HEADERS, timeout=30)
    found.raise_for_status()
    ids = ",".join(tool["id"] for tool in found.json()["data"]["results"])
    if not ids:
        return []
    fns = requests.get(
        f"{BASE}/v1/tools/functions",
        params={"ids": ids, "format": "openai"},
        headers=HEADERS,
        timeout=30,
    )
    fns.raise_for_status()
    return [
        StructuredTool.from_function(
            func=_runner(spec["function"]["name"]),
            name=spec["function"]["name"],
            description=spec["function"]["description"],
            args_schema=spec["function"]["parameters"],
        )
        for spec in fns.json()["data"]["tools"]
    ]
```

### Use them in an agent

```python
import os

from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from orcarouter_tools import orcarouter_tools

model = ChatOpenAI(
    model="openai/gpt-5-mini",
    base_url="https://api.orcarouter.ai/v1",
    api_key=os.environ["ORCAROUTER_API_KEY"],
)
agent = create_agent(model, tools=orcarouter_tools("tiktok user profile"))
result = agent.invoke({"messages": [{"role": "user", "content": "How many followers does @nasa have on TikTok?"}]})
print(result["messages"][-1].content)
```

## Recipes

- [Track competitor TikTok accounts](https://www.orcarouter.ai/tools/recipes/track-competitor-tiktok-accounts.md): Pull each competitor's TikTok profile, find its latest videos and read what viewers say, in three tool calls an agent can repeat every week.
- [Monitor brand mentions on the web and TikTok](https://www.orcarouter.ai/tools/recipes/monitor-brand-mentions.md): Search the web and TikTok for your brand, then read the comments under the posts that mention it: a daily brand check an agent can run on its own.
- [Enrich a lead list](https://www.orcarouter.ai/tools/recipes/enrich-a-lead-list.md): Turn a list of company domains into firmographics, a contact's work email and a recent news hook, with one API key.

## Related integrations

- [LlamaIndex](https://www.orcarouter.ai/tools/integrations/llamaindex.md): Load the OrcaRouter MCP server's tools with McpToolSpec and use them in a LlamaIndex FunctionAgent.
- [CrewAI](https://www.orcarouter.ai/tools/integrations/crewai.md): Give a CrewAI agent the OrcaRouter tool catalog through MCPServerAdapter, so it can find and run tools while it works on a task.
- [OpenAI Agents SDK](https://www.orcarouter.ai/tools/integrations/openai-agents-sdk.md): Wrap each OrcaRouter tool in a FunctionTool with its own JSON schema and run it from an Agents SDK agent, with the model served through OrcaRouter too.
- [Vercel AI SDK](https://www.orcarouter.ai/tools/integrations/vercel-ai-sdk.md): Turn OrcaRouter tools into AI SDK tools with jsonSchema() and let generateText call them over several steps.

---

Markdown version of https://www.orcarouter.ai/tools/integrations/langchain
