# LangChain

Source: https://staging-docs.aiand.com/integrations/langchain/

LangChain works against ai& through `ChatOpenAI` — point it at the ai& base URL and your existing chains, agents, and graphs work unchanged.

## Setup

```bash
pip install langchain langchain-openai
```

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="openai/gpt-oss-120b",
    base_url="https://api.aiand.com/v1",
    api_key="sk-your-api-key",
)

print(llm.invoke("Hello").content)
```

## In a chain

```python
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "Translate the user's input to {language}."),
    ("user", "{text}"),
])

chain = prompt | llm

print(chain.invoke({"language": "Japanese", "text": "Hello, world!"}).content)
```

## With tools

```python
from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"Sunny in {city}, 22°C"

llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Tokyo?")
print(response.tool_calls)
```

## LangGraph

LangGraph nodes that wrap `ChatOpenAI` work the same way — there's nothing ai&-specific. See [LangGraph docs](https://langchain-ai.github.io/langgraph/).

<Aside type="tip">
  For streaming, use `llm.stream(...)` or `chain.stream(...)`. Cost / request-id trailer events are accessible via the underlying response headers — see [Streaming Events](/reference/streaming-events/).
</Aside>
