Basic Usage
Pass your prompt directly via the-p flag:
Common Flags for Headless Mode
| Flag | Description |
|---|---|
-p "prompt" | The prompt to execute |
-a "agent" | Activate a sub-agent by name |
-w "workspace" | Select workspace by name |
--model | Choose model (opus, sonnet, haiku) |
--output-format | Output format (text, json, stream-json) |
--max-turns | Limit conversation turns |
Output Formats
Text (Default)
Returns the agent’s response as plain text:JSON
Returns structured JSON with the response and metadata:Stream JSON
Outputs JSON objects as they’re generated - useful for real-time processing:Using with Sub-Agents
Activate a specific agent for your headless task:Examples
Daily Task Summary
Export to File
Pipe to Other Commands
Conditional Logic
Environment Variables
Set API credentials via environment variables for server deployments:Workspace and model selection must be done via CLI flags (
-w, --model). Only API credentials support environment variables.Limitations
- No interactive prompts - the agent cannot ask clarifying questions
- Permission prompts are auto-denied unless
--allow-dangerousis set - OAuth flows requiring browser interaction will fail
Error Handling
Headless mode returns appropriate exit codes:| Exit Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Authentication error |
| 3 | Workspace not found |
Next Steps
Automation scripts
Build complete automation workflows with Craft Agents.
Want to run agents on a schedule? Set up daily briefings, automated reports, or CI jobs using GitHub Actions or cloud cron services. See Daily and Scheduled Jobs.