Question Intent Page · Updated 2026-06-19

How do I use an OpenAI-compatible API in LangChain?

Short answer

Use the same LangChain OpenAI-style chat model, but set base_url, api_key, model, timeout, max tokens, and callbacks explicitly. Test one tiny request first, then benchmark DeepSeek, Qwen, GLM, Groq/OpenRouter, or OpenLLMAPI. For production chains and agents, add route logs, fallback, and budget caps before retries or tools run unattended.

LangChain OpenAI compatible APILangChain base_url replacementQwen LangChain setupGLM LangChain API

Conclusion

  • Most LangChain migrations should be config changes, not a rewrite.
  • Compatible does not mean identical: test streaming, tools, JSON, embeddings, and error bodies.
  • Qwen and GLM setup should follow current official compatible-mode docs.
  • Gateway routing is useful when several chains, agents, or teammates share providers.

What to do next

  1. Move provider settings into environment variables: base_url, api_key, model, timeout, and max tokens.
  2. Run a minimal LangChain chat call outside your full app to confirm endpoint and key work.
  3. Test chain-specific features: streaming, structured output, tool calls, embeddings, rerank, and retries.
  4. Log tokens, provider, model, latency, retry count, final status, and user or workspace id.
  5. Use OpenLLMAPI when direct LangChain provider config becomes hard to audit across teams or features.

Recommended paths

Provider Free / credits Best for
DeepSeek Verify current pricing Low-cost LangChain reasoning workflows
Qwen DashScope Signup credits vary Official compatible mode for China-friendly apps
Zhipu GLM Signup tokens vary Domestic compatible endpoint experiments
OpenRouter/Groq Free routes vary Quick model comparison and demos
OpenLLMAPI Trial varies LangChain fallback, budgets, and route logs

Global developer checklist

  • Confirm whether signup, billing, and API keys work from your country before writing production code.
  • Prefer OpenAI-compatible endpoints when you may need to switch models, regions, or providers later.
  • Test free credits with a real smoke prompt and record latency, error shape, streaming behavior, and quota burn.
  • Keep at least one fallback route for provider outages, model deprecations, and regional access changes.

Production handoff

Give LangChain one audited model route

Keep compatible SDK settings while adding fallback, cost logs, and budget controls across chains, agents, and environments.

Set up LangChain routing →

FAQ

Can I just change base_url?

For basic chat, often yes. For production, also verify model names, auth headers, streaming, tool calls, structured output, and retries.

Why does LangChain still hit OpenAI?

A default provider, missing base_url, wrong environment variable, or wrapper preset may override your intended compatible endpoint.

Should I use Qwen, DeepSeek, or GLM?

Benchmark on your chain. DeepSeek is often cost-efficient, Qwen is strong for China-friendly workflows, and GLM is useful as domestic fallback.

When is a gateway better than direct provider config?

When you need one key, fallback, per-chain cost logs, budgets, or provider switching without redeploying every app.

🎁 Free Resource Pack

Get the Free AI Startup Toolkit

Free API credits list, AI business case studies, payment stack, risk checklist, and a monetization roadmap.

Get it free →
🐑 AI Assistant