# LangChain & LangGraph — API Store Hub

> VI: Cấu hình `ChatOpenAI` và `ChatAnthropic` trong LangChain/LangGraph trỏ về https://apistorehub.com để điều phối Router Agent, Tool Node và Fallback tự động.
> EN: Configure `ChatOpenAI` and `ChatAnthropic` inside LangChain and LangGraph against https://apistorehub.com for deterministic stateful agent graphs and model fallbacks.

- Canonical VI: https://apistorehub.com/vi/tich-hop/langchain-langgraph
- Canonical EN: https://apistorehub.com/en/tich-hop/langchain-langgraph
- Language: Python
- Runtime: Python 3.10+, langchain-openai, langgraph
- Protocol: OpenAI Compatible (/v1/chat/completions) & Anthropic (/v1/messages)
- Install: `pip install -U langchain-openai langgraph`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Install `langchain-openai` and `langgraph` in your Python environment.
2. Create a primary `claude-sonnet-5-5` client for reasoning and a `deepseek-v4-pro` client for routing or failover.
3. Chain `.with_fallbacks([fallback_llm])` for automatic recovery on transient 429 or 529 errors.
4. Enforce `recursion_limit` on graph invocation to cap runaway tool loops and ST spend.

## Production Code Sample
```
import os
from langchain_openai import ChatOpenAI
from langgraph.graph import END, MessagesState, StateGraph

primary_llm = ChatOpenAI(
    model="claude-sonnet-5-5",
    base_url="https://apistorehub.com/v1",
    api_key=os.environ["API_KEY"],
    temperature=0.2,
)
fallback_llm = ChatOpenAI(
    model="deepseek-v4-pro",
    base_url="https://apistorehub.com/v1",
    api_key=os.environ["API_KEY"],
)
resilient_llm = primary_llm.with_fallbacks([fallback_llm])

def agent_node(state: MessagesState):
    return {"messages": [resilient_llm.invoke(state["messages"])]}

graph = StateGraph(MessagesState)
graph.add_node("agent", agent_node)
graph.set_entry_point("agent")
graph.add_edge("agent", END)
app = graph.compile()
```

## Quick cURL Check
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-sonnet-5-5","messages":[{"role":"user","content":"Plan a 2-step LangGraph workflow"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `deepseek-v4-pro` | 3,600 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-pro.md |
| `gpt-6.2-sol` | 6,700 ST / 1M tokens | 1.1M | https://apistorehub.com/llms/models/gpt-6-2-sol.md |

## FAQ
- **Can LangGraph mix low-cost router models with flagship reasoning models on API Store Hub?**: Yes. Instantiate multiple `ChatOpenAI` nodes pointing to `https://apistorehub.com/v1` with different model IDs sharing one ST credit balance.
- **How do I configure `ChatAnthropic` instead of `ChatOpenAI`?**: Pass `base_url="https://apistorehub.com"` (without `/v1`) and your `sk-ash-` key to `ChatAnthropic`.
- **Does LangChain `with_structured_output` work?**: Yes, both strict JSON schema and tool-call extraction are supported across flagship models.
