FRAMEWORK INTEGRATION GUIDE 框架集成指南

LangChain + AI Gateway
Keep Orchestration and Routing Separate
LangChain + AI 网关:分离编排与路由职责

LangChain owns application orchestration; the gateway owns model access, policy and routing. Define that boundary before wiring callbacks, retries and tools.

LangChain 负责应用编排;网关负责模型访问、策略与路由。连接回调、重试与工具前,先定义边界。

LangChain AI gateway integration responsibilities, model routes and tool layer

TL;DR

Use a stable model interface

Point the LangChain model client at one approved gateway contract.

Register capabilities

Route only to models that satisfy tools, schema, context and stream requirements.

Choose one retry owner

Avoid nested LangChain, SDK and gateway retries.

Join callbacks and traces

Correlate chain steps, model routes, tool calls and final outcomes.

使用稳定模型接口

让 LangChain 模型客户端指向一个批准的网关契约。

注册能力

只路由到满足工具、结构定义、上下文与流要求的模型。

选择一个重试负责人

避免 LangChain、SDK 与网关嵌套重试。

连接回调与调用链

关联 Chain 步骤、模型路由、工具调用与最终结果。

Responsibility split 职责划分

LangChain assembles prompts, runnables, agents, retrievers and application state. The gateway authenticates requests, resolves model capabilities, routes, adapts providers and records inference evidence.

LangChain 组装提示词、Runnable、智能体、Retriever 与应用状态;网关认证请求、解析模型能力、路由、适配供应商并记录推理证据。

Tools remain application capabilities, not model endpoints. Give tool execution separate credentials, idempotency, timeouts and evidence even when a model requests the call.

工具属于应用能力,不是模型端点。即使由模型请求调用,也应为工具执行单独配置凭证、幂等、超时与证据。

Integration responsibilities 集成职责

Area 领域 Best fit 最适合 Verify before choosing 选择前验证
Prompt and chain 提示词与 Chain LangChain application code. LangChain 应用代码。 Version prompts and pass workload alias plus required capabilities. 版本化提示词,并传递工作负载别名与所需能力。
Model route 模型路由 AI gateway. AI 网关。 Record catalog, policy, selected endpoint and fallback attempts. 记录目录、策略、所选端点与故障切换尝试。
Retries 重试 One chosen layer. 一个选定层。 Share deadline and idempotency; disable overlapping retry loops. 共享截止时间与幂等,并禁用重叠重试循环。
Tools 工具 Application capability layer. 应用能力层。 Scope credentials, validate arguments and trace side effects. 限制凭证、验证参数并追踪副作用。

Integration checklist 集成检查清单

Connectivity

Verify client-to-gateway and gateway-to-provider paths.

Model contract

Test messages, tools, structured output, streams and errors.

Observability

Propagate trace context into callbacks and gateway logs.

Security

Keep provider and tool credentials out of chain state and prompts.

连通性

验证客户端到网关与网关到供应商路径。

模型契约

测试 Message、工具、结构化输出、流与错误。

可观测

把调用链上下文传播到回调与网关日志。

安全

让供应商与工具凭证远离 Chain State 与提示词。

Test the full chain 测试完整 Chain

  • Run deterministic chain inputs through every eligible model route.
  • Inject gateway timeout, provider failure and malformed tool output.
  • Confirm callbacks explain route, attempts, tools and final response.
  • Canary one chain and keep the previous model configuration ready.
  • 让确定性 Chain 输入经过每条合格模型路由。
  • 注入网关超时、供应商故障与异常工具输出。
  • 确认回调能解释路由、尝试、工具与最终响应。
  • 灰度一个 Chain,并保留旧模型配置。

LangChain orchestrates; the gateway governs inference LangChain 编排,网关治理推理

The chain sends a workload alias, required capabilities and trace context through a stable model interface. The gateway authorizes, selects and executes the model route. Tool requests return to the application capability layer; all spans join one workflow trace.

Chain 通过稳定模型接口发送工作负载别名、所需能力与调用链上下文;网关授权、选择并执行模型路由;工具请求返回应用能力层;所有追踪片段汇入一个工作流调用链。

Production rule: retry ownership and tool side effects must be explicit before production.

生产规则:生产前必须明确重试归属与工具副作用。

Use QVeris as the complementary tool capability layer 用 QVeris 作为互补工具能力层

The AI gateway handles inference. QVeris gives LangChain applications Discover → Inspect → Call access to external APIs, tools, services and live data. Share trace context without mixing model and tool credentials.

AI 网关处理推理;QVeris 为 LangChain 应用提供外部 API、工具、服务与实时数据的 Discover → Inspect → 调用。共享调用链上下文,但不要混合模型与工具凭证。

Wire LangChain to a gateway without losing evidence 将 LangChain 接到网关且不丢失证据

LangChain's current ChatOpenAI integration accepts a custom base_url for basic OpenAI-compatible chat. The official documentation warns that provider-specific response fields may not be preserved, so use a native provider package when your workload depends on non-standard reasoning, safety, or telemetry fields.

当前 LangChain ChatOpenAI 集成可通过自定义 base_url 连接基础 OpenAI 兼容 Chat。官方文档提醒,供应商特有响应字段可能不会被保留;如果工作负载依赖非标准推理、安全或遥测字段,应使用供应商原生包。

Minimal LangChain client 最小 LangChain 客户端
from langchain_openai import ChatOpenAI
import os

llm = ChatOpenAI(
    model=os.environ["LLM_MODEL_ID"],
    base_url=os.environ["LLM_GATEWAY_BASE_URL"],
    api_key=os.environ["LLM_GATEWAY_API_KEY"],
    timeout=20,
    max_retries=0,
    stream_usage=True,
)

message = llm.invoke("Return the word LANGCHAIN_OK")
print(message.content, message.usage_metadata, message.response_metadata)
  • Verify tool calls, strict structured output, streaming usage, and cancellation.
  • Capture LangChain run ID, gateway trace ID, native provider, exact model, and usage together.
  • Do not stack LangChain retries on top of gateway retries without a shared attempt budget.
  • 验证工具调用、严格结构化输出、流式用量与取消。
  • 把 LangChain Run ID、网关调用链 ID、原生供应商、准确模型与用量一起记录。
  • 没有共享 Attempt Budget 时,不要把 LangChain 重试叠加在网关重试之上。

Verified implementation reference: LangChain ChatOpenAI integration.

实施参考已根据官方资料核验:LangChain ChatOpenAI integration

Production Acceptance Checklist for LangChain AI gateway integrationLangChain AI Gateway 集成生产验收清单

For LangChain AI Gateway, a successful hello-world call proves connectivity only. Production acceptance must prove that the framework contract, model behavior, failures, evidence, and rollback remain predictable under the application’s real workflows.

针对“LangChain AI 网关”,Hello World 调用成功只能证明连接。生产验收还必须证明,在应用真实工作流中,框架契约、模型行为、失败处理、证据和回滚都保持可预测。

CONTRACT
Make configuration explicit and reversible
让配置显式且可逆

Version chat-model adapter behavior, callbacks, streaming, retries, structured output, tool calls, tracing, and gateway-side model aliases. Keep environment-specific secrets outside code, use stable internal model aliases, and document which layer owns transformations, retries, fallbacks, and timeout policy.

版本化聊天模型适配行为、回调、流式、重试、结构化输出、工具调用、追踪和网关侧模型别名。把环境专属密钥放在代码之外,使用稳定内部模型别名,并记录转换、重试、故障切换和超时策略分别由哪一层负责。

FIXTURES
Build an executable compatibility suite
建立可执行兼容测试集

To validate LangChain AI Gateway, cover system and user messages, long context, Unicode, streaming, structured output, tools, refusals, usage, cancellation, and provider errors. Store expected event order and required fields instead of checking text equality alone.

验证“LangChain AI 网关”时,覆盖系统与用户消息、长上下文、Unicode、流式、结构化输出、工具、拒绝、用量、取消和供应商错误,保存预期事件顺序与必需字段,而不只比较文本是否相同。

FAILURES
Inject the failures operators will see
注入运营人员真正会遇到的失败

To validate LangChain AI Gateway, test invalid credentials, unknown models, rate limits, slow first token, broken streams, malformed tool arguments, partial output, network loss, duplicate retries, and a provider outage. Confirm errors stay structured and side effects stay bounded.

验证“LangChain AI 网关”时,测试无效凭证、未知模型、限流、首 Token 过慢、流中断、工具参数畸形、部分输出、网络丢失、重复重试和供应商中断,确认错误保持结构化且副作用范围受控。

ROLLOUT
Canary with task-level evidence
使用任务级证据进行金丝雀发布

Before rolling out LangChain AI Gateway, start with a reversible workflow, compare completion quality, latency, cost, and trace coverage, then expand by task class. Define rollback signals, preserve the previous configuration, and assign an owner for drift and incident review.

上线“LangChain AI 网关”前,先选择可逆工作流,比较完成质量、延迟、成本和追踪覆盖,再按任务类别扩大。定义回滚信号,保留原配置,并明确负责漂移与事故复盘的人员。

FAQ

Does LangChain replace the gateway?

No. LangChain orchestrates application logic; the gateway governs model access.

Where should tools run?

In the application capability layer with scoped credentials and validation.

How should I trace it?

Propagate one workflow trace through chain, gateway, model and tool spans.

LangChain 会替代网关吗?

不会。LangChain 编排应用逻辑,网关治理模型访问。

工具应在哪里运行?

在应用能力层运行,并配置受限凭证与验证。

如何追踪?

让一个工作流调用链贯穿 Chain、网关、模型与工具追踪片段。

Official sources and further reading 官方资料与延伸阅读