Skip to main content

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
Headless mode executes a single prompt and exits, optionally outputting structured data.

Basic Usage

Output Formats

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

In headless mode with default approval, tools requiring approval will fail. Use yolo or auto-edit mode for automation.
Auto-approve all actions:
YOLO mode executes all actions without confirmation. Use with caution!

Environment Variables

Exit Codes

Advanced Examples

CI/CD Integration

Batch Processing

Automation Script

Timeouts and Limits

Debugging Headless Runs

Error Handling

Limitations

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.
Each invocation processes one prompt. For multi-turn conversations, use interactive mode.Workaround: Chain multiple invocations or use the ACP protocol for stateful sessions.
Progress indicators and rich formatting aren’t available.Workaround: Use stream-json output to implement your own progress tracking.
Headless sessions aren’t saved to disk unless explicitly configured.Workaround: Use --save-session flag or export output to a file.

Best Practices

  1. Always use --approval-mode yolo or auto-edit in automation
  2. Set reasonable --max-turns to prevent infinite loops
  3. Use --output stream-json for real-time processing
  4. Implement proper error handling with exit codes
  5. Set timeouts to prevent hanging processes
  6. Log debug output for troubleshooting
  7. Use environment variables for configuration
  8. 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