OpenAIDecisions exposes these decisions as a LangChain Runnable, so you can invoke, batch, or compose them with other runnables. It accepts strings, structured JSON, and LangChain message objects, and records traces and token usage in LangSmith. Use it for focused decisions such as routing a request, choosing a model, or checking whether a tool call is safe to run.
Use OpenAI decisions in LangChain middleware to control createAgent behavior at specific lifecycle points, including with custom middleware that classifies agent state. See Agent middleware for examples.
OpenAIDecisions and its middleware are in beta and require @langchain/openai>=1.8.0. The middleware also requires langchain>=1.6.0. Their APIs may change without notice.Setup
Optional: Set To route requests through the LangSmith LLM gateway, set
OPENAI_BASE_URL to use a compatible gateway or test server. The default is the OpenAI API endpoint.LANGSMITH_GATEWAY=true. Gateway requests authenticate with LANGSMITH_GATEWAY_API_KEY or LANGSMITH_API_KEY:Quickstart
Create anOpenAIDecisions instance with a decisions model, then pass the input and named questions together in each invocation. Questions that share input are answered in one request:
team.choice is typed as "infra" | "billing". Every question receives either an answer of its own type or a refusal, so check type before reading a typed field. The response also carries the model that answered, token usage in usage_metadata, and the OpenAI requestId.
The request input can be a string, a JSON object or array, or LangChain messages. Messages, including system, AI, and tool messages, are sent natively as a conversation, with tool calls and tool results as function call items. Text and image content is kept. Other content, such as files or audio, is replaced by a placeholder or omitted. JSON objects and arrays are sent as structured JSON in a single user message, and any messages nested inside them are converted the same way.
Decision types
Each question is one of three types:predicate is the only one without a confidence, since the probability is the answer. Use score rather than a predicate for a spectrum: a probability of 0.5 means an even split between yes and no, not “medium”.
score levels are ordered from lowest to highest and indexed from 0. Pass a plain string for a label-only level, or an object with a label and description. The returned score is the probability-weighted average of the level indices, so it may fall between levels. Each entry in probabilities holds a level’s index as value, its label, and its probability.
When the model declines to answer a question, the answer is a RefusalAnswer with type: "refusal" instead of a typed answer.
Agent middleware
Thelangchain package includes two beta middleware that put OpenAI decisions on an agent’s decision points. Import them from langchain. Install langchain alongside the OpenAI integration before using them:
Both middleware require a
model: a configured OpenAIDecisions instance.
Model routing
Classifies the latest human message across your named models and uses the selected one for every model call in the run. Each choice pairs a model with the criterion for picking it:modelRoute as an object with choice, probabilities, and confidence. When the model refuses, or the state has no human message, modelRoute is null and model calls use the agent’s own model.
Tool-risk gating
Asks for the probability that a tool call is risky or insufficiently authorized. Calls with a probability of0.5 or higher return an error ToolMessage instead of running the tool. Only the tools you list are classified. Pass their names or tool objects:
instructions to describe risk for your own tools. The middleware fails closed: refusals block the call, and API errors propagate without running the tool.
Custom middleware
OpenAIDecisions accepts LangChain message objects directly, so a custom middleware hook can classify an agent’s conversation state without converting it first. This example classifies the conversation once at the start of each run and stores the complete answer in agent state:
beforeModel to reclassify after each tool result, or wrapToolCall to classify a proposed action. See Custom middleware for all hooks and state patterns.
Tracing
OpenAI decisions are traced in LangSmith, so you can inspect decisions and token usage alongside the rest of your agent.See also
Connect these docs to your agent of choice via MCP for real-time answers.

