Unable to pass maxTokens and extract Resoning Summary in AzureChatOpenAI

Azure OpenAI: maxTokens Error and Reasoning Summary Extraction

I’m experiencing issues with Azure OpenAI using LangChain’s AzureChatOpenAI
class and wanted to share my findings.

The Problem

When using AzureChatOpenAI with GPT-5, I’m getting this error:

400 Unsupported parameter: ‘maxTokens’ is not supported with this model. Use
‘max_completion_tokens’ instead.

However, even when I rswitch to maxCompletionTokens, the error persists. Only after I remove maxTokens entirely from the constructor it works

Azure vs OpenAI Configuration Differences

I’ve noticed some key differences between ChatOpenAI and AzureChatOpenAI:

For OpenAI (works):

const model = new ChatOpenAI({
  model: "gpt-5",
  useResponsesApi: true,
  maxTokens: 16384,  // Works fine
});

const response = await model.invoke(messages, {
  reasoning: { effort: "low", summary: "detailed" }
});

For Azure (error with max_tokens):

const model = new AzureChatOpenAI({
    azureOpenAIApiKey: process.env.INTERNAL_AZURE_OPENAI_API_KEY,
    azureOpenAIApiInstanceName: "resource-name",
    azureOpenAIApiDeploymentName: "gpt-5",
    azureOpenAIApiVersion: "2024-10-01-preview",
    reasoning: { effort: "low" },  // Must be in constructor for Azure
    // Cannot use maxTokens or maxCompletionTokens - causes 400 error
  });

Questions

  1. Is maxTokens supported for Azure OpenAI GPT-5 deployments? The error suggests
    it isn’t, but I can’t find this documented anywhere.
  2. Can we extract reasoning summaries from Azure OpenAI? For OpenAI, passing
    reasoning to the invoke method works (as mentioned in the original post). For
    Azure, the documentation suggests putting reasoning in the constructor, but I
    cannot verify if reasoning summaries are actually extractable from
    additional_kwargs.
  3. Is this a LangChain bug? Could LangChain be sending max_tokens to Azure OpenAI
    even when it’s not specified in the constructor?

Environment

  • @langchain/openai: 0.6.14
  • Azure OpenAI API Version: 2024-10-01-preview
  • Model: gpt-5

Has anyone successfully used Azure OpenAI GPT-5 with LangChain and extracted
reasoning summaries? Any insights would be appreciated!

Hi @Nikfury

have you tried this:

import { AzureChatOpenAI } from "@langchain/openai";

const llm = new AzureChatOpenAI({
  azureOpenAIApiKey: process.env.INTERNAL_AZURE_OPENAI_API_KEY,
  azureOpenAIApiInstanceName: "resource-name",
  azureOpenAIApiDeploymentName: "gpt-5",
  azureOpenAIApiVersion: "2024-10-01-preview",
  useResponsesApi: true,
  maxTokens: 2048,
  reasoning: { effort: "low" },
});

const ai = await llm.invoke(messages, {
  reasoning: { summary: "detailed" },
});

Thanks @pawel-twardziak for responding again! I tested this exact code with my
Azure OpenAI gpt-5 deployment and here are the results:

  1. Your suggested code fails with 404 error:
const llm = new AzureChatOpenAI({
    azureOpenAIApiKey:
  process.env.INTERNAL_AZURE_OPENAI_API_KEY,
    azureOpenAIApiInstanceName:
  process.env.INTERNAL_AZURE_OPENAI_RESOURCE_NAME,
    azureOpenAIApiDeploymentName: "gpt-5",
    azureOpenAIApiVersion: "2024-10-01-preview",
    useResponsesApi: true,
    maxTokens: 2048,
    reasoning: { effort: "low" },
  });

  const ai = await llm.invoke(messages, {
    reasoning: { summary: "detailed" },
  });

Output:
error: { code: '404', message: 'Resource not found' }

  1. Commenting out specific parts makes it work:

Removing useResponsesApi, reasoning from initialization, AND
the reasoning parameter from invoke():

const llm = new AzureChatOpenAI({
    azureOpenAIApiKey:
  process.env.INTERNAL_AZURE_OPENAI_API_KEY,
    azureOpenAIApiInstanceName:
  process.env.INTERNAL_AZURE_OPENAI_RESOURCE_NAME,
    azureOpenAIApiDeploymentName: "gpt-5",
    azureOpenAIApiVersion: "2024-10-01-preview",
    // useResponsesApi: true,  ← Removed
    maxTokens: 2048,
    // reasoning: { effort: "low" },  ← Removed
  });

  const ai = await llm.invoke(messages);  // ← No reasoning 
  parameter

Output:

  ✓ Success without useResponsesApi!
  Response: "Response text"
  1. The same Azure deployment works perfectly with the
    official OpenAI SDK:
import { AzureOpenAI } from "openai";

  const client = new AzureOpenAI({
    apiKey: process.env.INTERNAL_AZURE_OPENAI_API_KEY,
    endpoint: `https://${process.env.INTERNAL_AZURE_OPENAI_RESO
  URCE_NAME}.openai.azure.com`,
    apiVersion: "2024-10-01-preview",
  });

  const response = await client.chat.completions.create({
    model: "gpt-5",
    messages: [{ role: "user", content: "what is the meaning of
   life, think hard" }],
    max_completion_tokens: 2048,
    reasoning_effort: "low",
  });

Output:

✓ Success with Responses API!
  Usage: {
    "reasoning_tokens": 192,  ← Reasoning IS working!
    "completion_tokens": 737,
    "total_tokens": 760
  }

Let me know if I am missing something or if you want me to try something else

Hi @Nikfury

try this:

const ai = await llm.invoke(messages, {
    reasoning: {
        effort: "high",
        summary: "auto" /* summary config if desired */,
    },
});

Your issue might be related to that How to extract GPT-5 reasoning summaries with @langchain/openai? - #15 by pawel-twardziak since it is still OpenAI class that is being exetended.

If you are still getting 404, that is another issue which you might need to debug.

Likely the 404 error is triggered by the source error. See that How to extract GPT-5 reasoning summaries with @langchain/openai? - #2 by pawel-twardziak

Hi @pawel-twardziak

let me know if im missing something but the solution you suggested in How to extract GPT-5 reasoning summaries with @langchain/openai? - #2 by pawel-twardziak. Suggests

  1. passing useResponsesApi: true in llm initialiation
  2. and passing reasoning config in invoke
reasoning: {
        effort: "high",
        summary: "auto" /* summary config if desired */,
    },
  • Using that with AzureChatOpenAI gives error: { code: '404', message: 'Resource not found' } however the same azure gpt-5 resource work if i remove both the above parameters but then I am not able to extract reasoning summary

  • Also, I am able to extract reasoning summar when calling the azure gpt-5 resource using offical OpenAI Responses API

Hi @Nikfury

bullets 1. and 2. should be sufficient.
If it’s still not working for AzureChatOpenAI … then I don’t actually knowy why.

Have you tried with ChatOpenAI from langchain? It should work following that How to extract GPT-5 reasoning summaries with @langchain/openai? - #14 by AMuresan