XGrammar-2: Fast, Customizable Structured Generation for Tool Calling and Agents MLC AI released XGrammar-2, a major upgrade to its constrained-decoding library that introduces Structural Tag, a composable JSON DSL for expressing OpenAI Harmony format, tool calling, reasoning channels, and custom output structures. XGrammar-2 has been adopted by xAI, Databricks, and DeepSeek, and is integrated by SGLang, vLLM, TensorRT-LLM, and MLC-LLM for strict tool calling, with efficiency optimizations including cross-grammar caching, repetition-state compression, and batching and speculative decoding support. The release targets agent applications that require complex output structures while maintaining 100% structural correctness and backward compatibility. TL;DR. XGrammar-2 is a major upgrade of XGrammar built for agent applications. It introduces Structural Tag , a composable JSON protocol that uniformly expresses OpenAI harmony format, tool calling, reasoning channels, and any custom output structure, exposed directly through serving engines’ API. Multiple efficiency optimizations , such as cross-grammar caching, repetition-state compression, and batching and speculative decoding support, ensure fast processing and minimal overhead even for huge structures. XGrammar-2 has been adopted by xAI, Databricks, DeepSeek, and other leading AI companies in their products. SGLang, vLLM, TensorRT-LLM, and MLC-LLM integrate it for strict tool calling and expose customization through API. Over the past year, agent applications, from Claude Code to OpenClaw, have grown rapidly in complexity. These systems define sophisticated harnesses that LLMs must interact with by producing specific output structures, such as tool calls and structured JSON. As these structures grow more complex, they pose greater challenges for LLMs to follow reliably. More than a year ago, we released XGrammar https://github.com/mlc-ai/xgrammar/ , which uses constrained decoding to guarantee 100% structural correctness with near-zero overhead. Since then, many organizations and open-source projects have adopted XGrammar, with active community discussion and contributions. While XGrammar already handles JSON and other common structures efficiently, emerging agent applications demand far more complex structures, raising new challenges in both flexibility and efficiency. To address these challenges, we are excited to introduce XGrammar-2 : a major upgrade purpose-built for agent applications. It lets you easily express complex structures for agents, delivers high performance even for very large grammars, offers native cross-platform APIs, and remains fully backward compatible. In this post, we first recap XGrammar and then walk through the key features of XGrammar-2. A Recap of XGrammar XGrammar uses constrained decoding to ensure LLM outputs conform 100% to a given structure. At each decoding step, constrained decoding produces a mask that blocks invalid tokens according to the structure. During sampling, invalid tokens are assigned zero probability, so only valid tokens will be generated. XGrammar’s key insight is precomputing an efficient token mask cache at compilation time, which substantially reduces mask generation time and achieves near-zero overhead during generation. XGrammar is best used to enforce format constraints, not to change the semantics of an LLM’s response. It helps downstream programs avoid fatal failures from malformed outputs, while keeping the impact on the model’s accuracy minimal. In our experiments, XGrammar ensured 100% valid tool-calling formats and, in many cases, improved tool-calling accuracy by eliminating format-related failures. Structural Tag: Abstraction for All Tool Calling and Complex Structures Agent applications are pushing LLMs to follow increasingly complex formats. One representative example is the OpenAI Harmony Format https://developers.openai.com/cookbook/articles/openai-harmony , which splits output into multiple channels, including reasoning, tool calling, and final response, each with its own format. Each open-source model also define their own tool calling formats. Supporting all of these requires significant effort from serving engines and downstream applications, and may still fail to match the official specification. XGrammar-2 introduces Structural Tag , a JSON-based DSL that provides a unified, lightweight, and extensible way to describe the diverse structures agents need, from OpenAI Harmony format to open-source model tool calling protocols and many other custom formats. For example, a DeepSeek V4 output with reasoning and a tool call looks like this: Let me check the weather in Beijing.