跪拜 Guibai
← All articles
Backend · Java · AIGC

Spring AI 2.0 GA: The control-plane shift that finally makes Java a first-class AI runtime

By Rain的Java大神之路 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

For Java teams building agentic AI, Spring AI 2.0 is the difference between gluing SDKs and running a governed pipeline: tool execution, memory, and observability can finally be placed where they can be audited and cost-controlled. The 34-64% token reduction and first-class MCP with load-balanced HTTP make production deployment viable, while the 42 breaking changes mean the upgrade decision is now tactical, not optional.

Summary

Spring AI 2.0 GA restructures how Java backends run AI agents. The tool-calling loop, previously buried inside each ChatModel implementation, is now a first-class citizen on the Advisor chain. That single architectural move turns an AI call from a black-box HTTP request into an assembleable pipeline where developers can insert memory, guardrails, retries, and cost controls at precise points. Official tests show token consumption drops 34-64% on OpenAI, Anthropic, and Gemini because tool definitions and loop management are no longer duplicated per model.

The release rides on Spring Boot 4.1, Framework 7, Jackson 3, and JDK 21 as the baseline, and it includes roughly 42 breaking changes. Tool annotations are renamed, options are immutable and replace rather than merge, temperature defaults are removed, and the provider count narrows from 20 to 7. MCP support moves from SSE to Streamable HTTP, making servers horizontally scalable. For teams running Spring AI 1.x, the migration path is explicit: new projects start on 2.0, old Boot 3 systems stay on the 1.x maintenance line, and anyone who must upgrade should isolate AI in a separate service.

The article also details production practices: ToolContext for server-side identity, eligibility checkers to cap tool-call loops, streaming rules that forbid retry after the first chunk, and Advisor ordering that determines whether you measure total user latency or per-call token cost. The message is that 2.0 gives Java the right hooks, but the actual governance — evaluation sets, cost baselines, security cut-points — still has to be built by the team.

Takeaways
— Spring AI 2.0 GA, after 8 milestones and 2 RCs, runs on Spring Boot 4.1, Framework 7, Jackson 3, Jakarta EE 11, JSpecify, with JDK 21 as the practical baseline.
— Spring Boot 3.5 and Framework 6.2 reach EOL on 2026-06-30, 18 days after 2.0 GA, so waiting is no longer viable.
— The core refactor: tool-calling loop moves from inside each ChatModel to the Advisor chain, making it a first-class, assembleable pipeline.
— Official tests show a 34-64% token consumption reduction on OpenAI, Anthropic, and Gemini with the new tool-calling architecture.
— ToolContext is a trusted channel: data in it is never sent to the model, so server-side identity like userId belongs there.
— Do not manually register ToolCallingAdvisor in 2.0; DefaultChatClient auto-registers it, and a duplicate registration doubles tool execution.
— conversationId must be explicitly passed; DEFAULT_CONVERSATION_ID is removed, and omission throws an exception by design.
— Tool descriptions benefit from concrete examples (e.g., ORD-1001) over abstract definitions for better model parameter accuracy.
— ToolExecutionEligibilityChecker is the new hook to cap tool-call loops: max rounds, total timeout, and token budget are recommended.
— MCP Java SDK 2.0 defaults to Streamable HTTP, allowing stateless, load-balanced MCP servers; SSE long-connections are no longer the default.
— The MCP header validation in 2.0 is lowercase-only, so read headers with headers.get("authorization") to avoid 401s.
— Jackson 3 moves package names to tools.jackson.*, flips several serialization defaults, and makes exceptions unchecked.
— Anthropic maxTokens default changes from 500 to 4096 in 2.0, affecting cost and response length.
— Streaming rule: retry is allowed only before the first chunk; after first chunk, continue writing and use requestId for idempotent assembly.
— Advisor order determines what you measure: outer order (+10) captures real user latency; inner order (+400) captures per model call, where multi-turn token costs hide.
— For testing, mock at the ChatModel layer, not ChatClient, and assert tool-call trajectories rather than plain text.
— Virtual threads on JDK 21 pin carrier threads inside synchronized blocks and native calls; use ReentrantLock on hot paths.
— The migration cost for a real project: about 3 hours for compile errors, 5 hours for behavior diff investigation.
— For fixed-step approval flows, a state machine is cheaper and more controllable than an Agent; agents are only for variable-step scenarios.
— 2.1 is expected to bring session memory, tool call confirmation (HITL), a DecisionModel API, and MCP modern protocol.
Conclusions

The real win of 2.0 is not the token savings but that Java finally has a named place to put guardrails: before this, security and cost controls were either bolted on outside the model or written ad hoc inside services.

Removing the temperature default and changing Anthropic's maxTokens silently alters output behavior after upgrade; teams can't rely on monitoring alone, and A/B comparisons will surface the drift first.

The narrowing of providers from 20 to 7 signals a maturity cut: Spring AI stops pretending to be a universal adapter and focuses on the vendors where it can actually test and guarantee integration.

ToolContext being invisible to the model is a design pattern worth copying: any secret, identity, or non-negotiable parameter belongs in a channel the model cannot influence.

The explicit conversationId requirement is a small breaking change that eliminates a whole class of multi-tenant session bugs, which shows how deliberate the 2.0 team is about production semantics.

The 34-64% token reduction is attributed to removing per-model duplication, which implies that a significant share of token waste in 1.x came from framework overhead, not from model behavior.

Concepts & terms
Advisor chain
In Spring AI 2.0, an AI call passes through a chain of Advisors, analogous to Spring MVC interceptors. Each Advisor can inspect or modify the request before the model call, including tool-calling, memory, and guardrail logic. This makes agent behavior assembleable and governable.
ToolContext
A server-side container passed to tool methods that carries data never sent to the model. Used for user identity, trace IDs, or any context the model should not see or modify. It is the trusted channel between the Java backend and tool execution.
ToolExecutionEligibilityChecker
A hook in Spring AI 2.0's ToolCallingAdvisor that lets the application decide whether to continue the tool-call loop. It can enforce maximum rounds, elapsed time, or token budgets, effectively preventing agent runaway loops.
MCP (Model Context Protocol)
An open protocol for exposing tools, resources, and prompts to AI models. In Spring AI 2.0, MCP is integrated into the framework with annotations like @McpTool, and the transport defaults to Streamable HTTP instead of SSE, making servers horizontally scalable.
Virtual thread pinning
On JDK 21, a virtual thread executing inside a synchronized block or native call can pin the underlying carrier thread, reducing concurrency benefits. JEP 491 fixes this; until then, use ReentrantLock in hot paths.
TTFT (Time to First Token)
The latency from sending a request to receiving the first token of a model's response. It is the user-perceived metric for streaming AI, more meaningful than total P99 latency.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗