After following the quickstart document to build a real word agent, it aways throws this error:
400 An assistant message with ‘tool_calls’ must be followed by tool messages responding to each ‘tool_call_id’
I think this might be related to that pending bug fix fix(openai): pair reasoning with function_call id; add tests by pawel-twardziak · Pull Request #9082 · langchain-ai/langchainjs · GitHub
I will check it.
Hmmm the example works for me:
{
messages: [
HumanMessage {
"id": "29710469-f7bd-4e30-a0e8-998c35b8173d",
"content": "What's the weather in Tokyo?",
"additional_kwargs": {},
"response_metadata": {}
},
AIMessage {
"id": "msg_01YAxpwQzBjL6MZqqpjHPsQH",
"content": [
{
"type": "tool_use",
"id": "toolu_015E8qECr7A8AkFGHAqUYnnX",
"name": "get_weather",
"input": {
"city": "Tokyo"
}
}
],
"name": "model",
"additional_kwargs": {
"model": "claude-sonnet-4-5-20250929",
"id": "msg_01YAxpwQzBjL6MZqqpjHPsQH",
"type": "message",
"role": "assistant",
"stop_reason": "tool_use",
"stop_sequence": null,
"usage": {
"input_tokens": 606,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"cache_creation": {
"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0
},
"output_tokens": 53,
"service_tier": "standard"
}
},
"response_metadata": {
"model": "claude-sonnet-4-5-20250929",
"id": "msg_01YAxpwQzBjL6MZqqpjHPsQH",
"stop_reason": "tool_use",
"stop_sequence": null,
"usage": {
"input_tokens": 606,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"cache_creation": {
"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0
},
"output_tokens": 53,
"service_tier": "standard"
},
"type": "message",
"role": "assistant",
"model_provider": "anthropic"
},
"tool_calls": [
{
"name": "get_weather",
"args": {
"city": "Tokyo"
},
"id": "toolu_015E8qECr7A8AkFGHAqUYnnX",
"type": "tool_call"
}
],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 606,
"output_tokens": 53,
"total_tokens": 659,
"input_token_details": {
"cache_creation": 0,
"cache_read": 0
}
}
},
ToolMessage {
"id": "1ee82833-84f4-4f5e-8482-36e27f09b114",
"content": "It's always sunny in Tokyo!",
"name": "get_weather",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "toolu_015E8qECr7A8AkFGHAqUYnnX"
},
AIMessage {
"id": "msg_01JHisN4bK4L6j129EQ7WCtW",
"content": "The weather in Tokyo is sunny! ☀️",
"name": "model",
"additional_kwargs": {
"model": "claude-sonnet-4-5-20250929",
"id": "msg_01JHisN4bK4L6j129EQ7WCtW",
"type": "message",
"role": "assistant",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 678,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"cache_creation": {
"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0
},
"output_tokens": 14,
"service_tier": "standard"
}
},
"response_metadata": {
"model": "claude-sonnet-4-5-20250929",
"id": "msg_01JHisN4bK4L6j129EQ7WCtW",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 678,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"cache_creation": {
"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0
},
"output_tokens": 14,
"service_tier": "standard"
},
"type": "message",
"role": "assistant",
"model_provider": "anthropic"
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 678,
"output_tokens": 14,
"total_tokens": 692,
"input_token_details": {
"cache_creation": 0,
"cache_read": 0
}
}
}
]
}
@DeepenLau have you changed the provider for OpenAI? Or any changes to the code?
For openai:gpt-5 it also works for me:
{
messages: [
HumanMessage {
"id": "9f0c1e16-87d1-429e-8a32-041974071a99",
"content": "What's the weather in Tokyo?",
"additional_kwargs": {},
"response_metadata": {}
},
AIMessage {
"id": "chatcmpl-CT6x0kWc3KJnZHt9Ph1VbOR5ZcCKE",
"content": "",
"name": "model",
"additional_kwargs": {
"tool_calls": [
{
"id": "call_rqQIxuJLO81Jaf69mfwni2V2",
"type": "function",
"function": "[Object]"
}
]
},
"response_metadata": {
"tokenUsage": {
"promptTokens": 141,
"completionTokens": 151,
"totalTokens": 292
},
"finish_reason": "tool_calls",
"model_provider": "openai",
"model_name": "gpt-5-2025-08-07"
},
"tool_calls": [
{
"name": "get_weather",
"args": {
"city": "Tokyo"
},
"type": "tool_call",
"id": "call_rqQIxuJLO81Jaf69mfwni2V2"
}
],
"invalid_tool_calls": [],
"usage_metadata": {
"output_tokens": 151,
"input_tokens": 141,
"total_tokens": 292,
"input_token_details": {
"audio": 0,
"cache_read": 0
},
"output_token_details": {
"audio": 0,
"reasoning": 128
}
}
},
ToolMessage {
"id": "040c7f44-3629-444b-93d2-203265386898",
"content": "It's always sunny in Tokyo!",
"name": "get_weather",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "call_rqQIxuJLO81Jaf69mfwni2V2"
},
AIMessage {
"id": "chatcmpl-CT6x6kv44j5BlB3KNc7EjsoSmFxaj",
"content": "It’s always sunny in Tokyo!",
"name": "model",
"additional_kwargs": {},
"response_metadata": {
"tokenUsage": {
"promptTokens": 176,
"completionTokens": 592,
"totalTokens": 768
},
"finish_reason": "stop",
"model_provider": "openai",
"model_name": "gpt-5-2025-08-07"
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"output_tokens": 592,
"input_tokens": 176,
"total_tokens": 768,
"input_token_details": {
"audio": 0,
"cache_read": 0
},
"output_token_details": {
"audio": 0,
"reasoning": 576
}
}
}
]
}
I changed the provider for to deepseek, and this error is throw when call the second times of agent.invoke(). The first time is work
How are you invoking it second time? Have you modified the code? What’s the invocations flow?

