文档 · 指南

Azure OpenAI 接入 Codex、Claude Code 等 Agent

Azure OpenAI 在你自己的 Azure 资源上提供 OpenAI 的模型,以及 Kimi、DeepSeek、Grok 等其他厂商的模型。magpie 的 azure 预设把这个资源接到 Codex、Claude Code、OpenCode 等 Agent:接口地址和 Key 都是你资源自己的,模型 id 就是你部署的名字。

添加

  1. 找到地址和 Key。在 Azure 门户里打开资源的「密钥和终结点」,复制终结点和一个 Key。
  2. 添加。在应用里选 Azure OpenAI,把地址填进 接口地址,或者在命令行:
终端
magpie provider add azure  url=https://my-resource.openai.azure.com
magpie provider test azure

地址怎么贴都行:https://<资源>.openai.azure.com、…/openai、…/openai/v1、带 api-version 的旧版部署 URL,或者只写资源名。magpie 一律改用资源的 v1 接口(/openai/v1,Azure 自 2025 年 8 月起正式提供),Chat Completions、Responses 和模型列表都在 OpenAI 同样的路径下,不需要 api-version。.openai.azure.com、.cognitiveservices.azure.com 和 .services.ai.azure.com 这三种主机都认。

Key 放在 api-key 请求头里发送,从不作为 Bearer 发:Azure 上的 Bearer 是 Entra ID 令牌。

部署就是模型

添加后,magpie 用这把 Key 向资源要部署列表,部署的名字就是模型 id。数据面不给列表时,就用 v1 接口列出的模型,这只有在每个部署都以模型命名时才对得上。两个都拿不到时,错误会提示你手动填部署名:

终端
magpie provider models azure my-gpt-5-5 my-gpt-6-sol

Azure 上也有其他厂商的模型,它们在 models.dev 里没有推理档位,所以 magpie 不把这个列表当作厂商自己的说法。

给 Agent 用

Codex 走 Responses,Claude Code 等说 Anthropic 协议的 Agent 由 magpie 转换成 Azure 的接口:

终端
magpie codex azure/my-gpt-5-5
magpie claude azure/my-gpt-5-5

API Management 和请求头

资源前面套了 API Management 网关(或其他不是资源主机的地址)时,magpie 保留你给的 URL,不改到 v1 接口。在编辑器的 请求头 里可以加自定义头,值里的 ${sessionId}、${threadId} 和 ${subagentId} 会换成当前对话的 id,方便网关把同一段对话固定到同一个账号:

终端
magpie provider set azure 'header.X-Session=${sessionId}'

几点说明

常见问题

magpie 支持 Azure OpenAI 吗?

支持,用 azure 预设。填上资源的接口地址和 Key,magpie 走 /openai/v1 的 v1 接口,Key 放在 api-key 请求头里,把资源的部署列为模型。

模型 id 填什么?

填你部署的名字,不是模型名。magpie 会向资源要部署列表;拿不到时,用 magpie provider models azure <部署名…> 手动填入。

需要填 api-version 吗?

不需要。magpie 用 Azure 的 v1 接口,旧版的 /openai/deployments/…?api-version= 地址也会改到 v1 接口。

资源前面有 API Management 网关怎么办?

直接填网关的地址,magpie 不会改它。需要的头在请求头里加,${sessionId} 等会换成当前对话的 id。

相关页面:OpenAI 接入 Claude Code、Codex、Gemini CLI 和 OpenCode、Amazon Bedrock 接入 Claude Code、Codex 等 Agent。遇到错误请看 常见错误排查:401、429、502、504 等,或者到 Discord 提问。