---
url: 'https://docs.byesu.com/en/clients/langchain.md'
description: >-
  Configure ChatOpenAI and ChatAnthropic with byesu API in LangChain and
  LangGraph: run Claude 5.5, GPT-6.2, Gemini 4, and DeepSeek V4 tool agents.
---

# Connect LangChain & LangGraph to byesu API

Connect **LangChain** and **LangGraph** (Python) to **byesu** via `langchain-openai` or `langchain-anthropic` to orchestrate multi-vendor tool-calling chains, structured output pipelines, and stateful ReAct agents with one pay-as-you-go API key.

::: tip Quick configuration

* **`ChatOpenAI` (`langchain-openai`) `base_url`**: `https://byesu.com/v1` (all vendors under one class)
* **`ChatAnthropic` (`langchain-anthropic`) `anthropic_api_url`**: `https://byesu.com` (**without `/v1`** — native `/v1/messages` + `0.1x` cache reads)
* **Environment variable**: `BYESU_API_KEY=sk-...` (created in the default `auto` group)
* **Recommended models**: `gpt-6.2-sol`, `claude-opus-5-5`, `claude-sonnet-5-5`, `deepseek-v4-pro`, `gemini-4-argon`
  :::

## Prerequisites

1. Generate an `sk-...` token in the [byesu Console](https://byesu.com) under the default **`auto`** group (see [Choosing a Group](/en/guide/groups)).
2. Install the official partner packages:

```bash
pip install langchain-openai langchain-anthropic langgraph
export BYESU_API_KEY="sk-your-byesu-token"
```

## Option 1: `langchain-openai` (All Models via `ChatOpenAI`)

Use `ChatOpenAI` with `base_url="https://byesu.com/v1"` when you want a single interface that can hot-swap between GPT, Claude, DeepSeek, Gemini, and Grok model IDs:

```python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-6.2-sol",  # or "claude-sonnet-5-5", "deepseek-v4-pro", "gemini-4-argon"
    api_key=os.environ["BYESU_API_KEY"],
    base_url="https://byesu.com/v1",
    timeout=120,
)

resp = llm.invoke("Summarize three failure modes of distributed locks in bullet points.")
print(resp.content)
```

For OpenAI `/v1/responses` features on `gpt-6.2-sol` or `gpt-6.2-astra`, pass `use_responses_api=True` on `ChatOpenAI`.

## Option 2: `langchain-anthropic` (Native `/v1/messages` & `0.1x` Caching)

For Claude-heavy agent graphs that benefit from native Anthropic tool blocks, adaptive thinking blocks, and `cache_control` breakpoints, use `ChatAnthropic`:

::: warning Root URL without `/v1` in `langchain-anthropic`
`ChatAnthropic` wraps the official `anthropic` Python SDK, which appends `/v1/messages` automatically. Pass **`anthropic_api_url="https://byesu.com"` without `/v1`** — adding `/v1` hits `/v1/v1/messages` and returns `404`.
:::

```python
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, SystemMessage

claude = ChatAnthropic(
    model="claude-opus-5-5",
    api_key=os.environ["BYESU_API_KEY"],
    anthropic_api_url="https://byesu.com",
    timeout=120,
)

# Optional: mark a 1,024+ token static system prompt for 0.1x cache reads
messages = [
    SystemMessage(
        content=[
            {
                "type": "text",
                "text": "You are a principal SRE reviewing Kubernetes manifests...",
                "cache_control": {"type": "ephemeral"},
            }
        ]
    ),
    HumanMessage(content="Check whether PodDisruptionBudget minAvailable=1 blocks node drains on single-replica deployments."),
]

reply = claude.invoke(messages)
print(reply.content)
print("Usage metadata:", reply.usage_metadata)
```

See [Prompt Caching](/en/guide/prompt-caching) for prefix ordering rules that preserve **`0.1x`** cache reads across agent iterations.

## LangGraph ReAct Agent with `@tool` Binding

Both `ChatOpenAI` and `ChatAnthropic` plug directly into LangGraph's tool-calling workflows:

```python
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent


@tool
def get_queue_depth(queue_name: str) -> dict:
    """Return current backlog depth and oldest message age in seconds for a worker queue."""
    return {"queue": queue_name, "pending_jobs": 1420, "oldest_age_sec": 94}


model = ChatAnthropic(
    model="claude-sonnet-5-5",
    api_key=os.environ["BYESU_API_KEY"],
    anthropic_api_url="https://byesu.com",
    timeout=120,
)

agent = create_react_agent(model, tools=[get_queue_depth])
result = agent.invoke(
    {"messages": [("user", "Inspect the billing-webhooks queue and tell me if we should scale consumers.")]}
)
print(result["messages"][-1].content)
```

## Production Engineering Notes

* **Do not hardcode `temperature` on Claude 5.5 (`2026-10-21`) or GPT-6.2 reasoning models**: Many older LangChain examples pass `temperature=0` or `temperature=0.7`. Anthropic API version `2026-10-21` rejects `temperature`, `top_p`, and `top_k` with HTTP `400`, and GPT-6.2 reasoning models likewise reject fixed sampling overrides. Omit `temperature` (or pass `temperature=None` if an older wrapper sets a float default) — see [Claude 400 temperature/top\_p fix](/en/errors/claude-api-400-temperature-top-p-not-supported) and [unsupported `max_tokens`](/en/errors/openai-unsupported-parameter-max-tokens).
* **Timeouts for multi-step graphs**: Keep `timeout=120` (or higher) on `ChatOpenAI` / `ChatAnthropic` and stream graph updates (`agent.stream(..., stream_mode="values")`) when invoking deep-reasoning models (`gpt-6.2-sol`, `claude-opus-5-5`, `deepseek-v4-pro`).
* **Token groups & billing**: The default `auto` group covers Claude, GPT, Gemini, DeepSeek, and Grok. Issue separate tokens in `Glm` (`glm-5.3`), `Kimi` (`kimi-k3`), or `media` (`gpt-image-2`, `gemini-3.1-flash-image-ultra`) when needed. Usage is pay-as-you-go per token with live rates shown in the [byesu Console](https://byesu.com) after sign-in.

## Troubleshooting & Benchmarks

* **`401 authentication_error` / `invalid_api_key`**: verify `base_url="https://byesu.com/v1"` on `ChatOpenAI` vs `anthropic_api_url="https://byesu.com"` on `ChatAnthropic` ([OpenAI 401 fix](/en/errors/openai-api-401-invalid-api-key), [Claude 401 fix](/en/errors/claude-api-401-authentication-error)).
* **`404` / `no available channel`**: ensure the `model` string matches the console ID character for character and matches your token group ([no available channel](/en/errors/no-available-channel-error)).
* **Timeouts or dropped streams**: increase client `timeout` to `>= 120`s ([Anthropic timeout](/en/errors/anthropic-api-timeout), [streaming connection dropped](/en/errors/streaming-connection-dropped)).
* **`429` & latency metrics**: see [OpenAI 429 rate limit vs quota](/en/errors/openai-api-429-rate-limit-vs-quota) and [weekly production benchmarks](https://byesu.com/benchmarks).
