Carry tool-call requests
An LLM requests an external operation such as weather lookup or database access.
AI Agents · Class
AIMessage represents an AI response in LangGraph.js. It can include content, typed tool calls, invalid tool calls, usage metadata, and an optional example marker.
AIMessage is a LangChain message type representing an AI response. In LangGraph.js it can carry ordinary content, tool-call requests, invalid tool calls, usage metadata, and an optional custom tool-call type parameter that enables type-safe handling of tool names and arguments.
type AIMessage<ToolCall = DefaultToolCall> = BaseMessage & { example?: boolean; invalid_tool_calls?: InvalidToolCall[]; tool_calls?: ToolCall[]; type: "ai"; usage_metadata?: UsageMetadata; };| Name | Type | Description |
|---|---|---|
ToolCall | type parameter | Optional generic type describing the tool-call objects stored in the message; it defaults to DefaultToolCall. |
An AIMessage value: a BaseMessage with AI-specific fields and an optional, parameterized tool-call collection.
import { AIMessage } from "@langchain/core/messages";
type WeatherToolCall = {
name: "get_weather";
args: { location: string };
id: string;
type: "tool_call";
};
type MyToolCalls = WeatherToolCall;
const message: AIMessage<MyToolCalls> = new AIMessage({
content: "",
tool_calls: [{
name: "get_weather",
args: { location: "San Francisco" },
id: "call_1",
type: "tool_call",
}],
});
for (const tool of message.tool_calls ?? []) {
if (tool.name === "get_weather") console.log(tool.args.location);
}Creates an AI message with a typed weather tool call and safely reads its location.
An LLM requests an external operation such as weather lookup or database access.
A TypeScript application needs compile-time checking of tool names and argument objects.
An application records token counts or other provider usage details with each model response.
AIMessage is a type alias combining BaseMessage with AI-specific fields. Its discriminator is type: "ai", and it can include tool_calls, invalid_tool_calls, usage_metadata, and an optional example flag.
Follow-up: How would you preserve type safety for several tools? Define a discriminated union of tool-call variants and pass that union as the AIMessage generic parameter.
The generic parameter controls the type of tool calls stored on the message. It defaults to DefaultToolCall, but callers can provide a custom type to make tool names and argument objects type-safe.
Follow-up: What does narrowing look like? Check a discriminating field such as tool.name, then TypeScript narrows the corresponding args shape.
A tool-call collection is optional, so code should check whether tool_calls exists before iterating it. Once present, each call exposes the custom tool-call structure selected by the generic parameter.
Follow-up: How should malformed requests be represented? Use invalid_tool_calls when the response contains tool-call data that could not be parsed into valid calls.
BaseMessageDefaultToolCallInvalidToolCallToolCallUsageMetadata