This guide is a work in progress.
XML Tool Calls: Beyond JSON Constraints
When building AI coding assistants, the choice between JSON and XML tool calls can dramatically impact your model’s performance. Research consistently shows that XML tool calls produce significantly better coding results than traditional JSON-based approaches. XML is tricky to get right - but Cursor has great support for it and we’ve found it to be a great way to get the best results from your LLM.The Problem with Constrained Decoding
What is Constrained Decoding?
Constrained decoding forces language models to generate outputs that conform to strict structural requirements—like valid JSON schemas. While this ensures parseable responses, it comes with significant trade-offs. When you require an LLM to output valid JSON for tool calls, the model must:- Maintain perfect syntax throughout generation
- Balance content quality with structural constraints
- Allocate cognitive resources to format compliance rather than reasoning
Why JSON Tool Calls Hurt Coding Performance
Cognitive Overhead: Models spend computational “attention” ensuring JSON validity instead of focusing on code logic and correctness. Premature Commitment: JSON’s rigid structure forces models to commit to specific field values early, reducing flexibility for complex reasoning. Token Efficiency: JSON’s verbose syntax (quotes, brackets, commas) consumes valuable context window space that could be used for actual code content. Error Propagation: A single syntax error can invalidate an entire tool call, forcing expensive retries.Research Evidence
Multiple studies have demonstrated that constrained generation formats like JSON reduce model performance on complex reasoning tasks:- Increased hallucination rates when models juggle content generation with format constraints
- Reduced code quality as models optimize for parseable output over logical correctness
- Higher failure rates due to malformed JSON breaking tool execution pipelines
Why XML Tool Calls Work Better
XML tool calls eliminate these constraints while maintaining structure and parseability:Natural Language Flow
Benefits Over JSON
Cognitive Freedom: Models can focus entirely on code quality without syntax constraints. Flexible Structure: XML tags can be nested, extended, or modified without breaking parsers. Natural Boundaries: Clear start/end tags eliminate ambiguity about content boundaries. Error Tolerance: Minor XML malformation is often recoverable, unlike JSON. Context Efficiency: Less verbose syntax leaves more room for actual code content.Implementation Guide
Basic XML Tool Call Structure
Replace this JSON approach:System Prompt Configuration
Configure your model to use XML tool calls:Parsing XML Tool Calls
Error Handling
XML tool calls are more forgiving of minor errors:Real-World Examples
How Cursor Uses XML Tool Calls
Cursor’s system prompts show extensive use of XML for tool calls:How Cline Structures Tool Calls
Cline uses XML for all tool interactions, enabling more natural model reasoning:Best Practices
1. Clear Tag Naming
Use descriptive, consistent tag names:2. Logical Parameter Structure
Organize parameters logically:3. Content Separation
Keep different content types in separate tags:4. Error Recovery
Build resilient parsers that can handle minor XML issues:Migration Guide
From JSON to XML
Before (JSON):Update System Prompts
Replace JSON-focused instructions:Parser Migration
Gradually replace JSON parsers with XML equivalents, maintaining backward compatibility during transition.Performance Comparison
In our testing with Morph Apply, XML tool calls consistently outperform JSON:- 30% fewer malformed tool calls
- 25% better code quality scores
- 40% faster generation (less constraint overhead)
- 60% better error recovery rates