Overview
Headless mode allows you to run Qwen Code without the interactive UI, making it ideal for:- CI/CD pipelines
- Automation scripts
- Batch processing
- Integration with other tools
- Programmatic access
Basic Usage
Output Formats
- Text (Default)
- JSON
- Stream JSON
Human-readable text output:Output:
Input Formats
Standard Input
File Input
Use@include to reference files:
Stream JSON Input
For advanced integrations:Approval Modes in Headless
- YOLO Mode
- Auto Edit Mode
- Plan Mode
Auto-approve all actions:
Environment Variables
Exit Codes
Advanced Examples
CI/CD Integration
Batch Processing
Automation Script
Timeouts and Limits
Debugging Headless Runs
Error Handling
- Shell Script
- Python
- Node.js
Limitations
No user input prompts
No user input prompts
Tools that require user input (like
ask_user_question) will fail in headless mode.Workaround: Use YOLO mode to skip confirmations, or provide input upfront.Limited to single prompt
Limited to single prompt
Each invocation processes one prompt. For multi-turn conversations, use interactive mode.Workaround: Chain multiple invocations or use the ACP protocol for stateful sessions.
No visual feedback
No visual feedback
Progress indicators and rich formatting aren’t available.Workaround: Use
stream-json output to implement your own progress tracking.Session not persisted by default
Session not persisted by default
Headless sessions aren’t saved to disk unless explicitly configured.Workaround: Use
--save-session flag or export output to a file.Best Practices
- Always use
--approval-mode yoloorauto-editin automation - Set reasonable
--max-turnsto prevent infinite loops - Use
--output stream-jsonfor real-time processing - Implement proper error handling with exit codes
- Set timeouts to prevent hanging processes
- Log debug output for troubleshooting
- Use environment variables for configuration
- Test scripts locally before deploying to CI/CD
Next Steps
Approval Modes
Understand permission modes for automation
Session Commands
Learn available commands (some work in headless)
MCP Integration
Extend with Model Context Protocol servers
Interactive Mode
Switch to interactive UI for complex tasks
