Exec mode enables non-interactive, headless execution of Cortex for automation, CI/CD pipelines, and scripting workflows.
Exec mode is designed for scenarios where human interaction is not available or desired:
- CI/CD Pipelines - Automated code review, testing, and deployment
- Shell Scripts - Batch processing and automation
- Scheduled Tasks - Cron jobs and periodic maintenance
- Programmatic Integration - API-like usage from other tools
Key characteristics:
- One-shot execution: complete the task and exit
- Configurable autonomy levels for safety
- Structured output formats (text, JSON, JSONL, Markdown)
- Timeout and turn limits to prevent runaway execution
cortex exec "your prompt here"cortex exec -f prompt.txt
cortex exec --file prompt.txtecho "your prompt" | cortex exec
cat instructions.md | cortex exec# Prompt with context from stdin
echo "additional context" | cortex exec "analyze this code"| Flag | Description |
|---|---|
<prompt> |
The prompt to execute (positional argument) |
-f, --file <PATH> |
Read prompt from a file |
--input-format <FORMAT> |
Input format for multi-turn: text, json |
--clipboard |
Read input from clipboard |
--git-diff |
Include git diff in context |
--include <PATTERN> |
Include files matching pattern in context |
--exclude <PATTERN> |
Exclude files matching pattern |
--url <URL> |
Fetch and include URL content in context |
-i, --image <PATH> |
Attach image file (can be used multiple times) |
| Flag | Description |
|---|---|
-o, --output-format <FORMAT> |
Output format: text (default), json, jsonl, markdown |
--echo |
Include the prompt in the output |
--response-format <FORMAT> |
Response format: text, json, json_object |
--output-schema <SCHEMA> |
JSON schema for structured output |
--logprobs <N> |
Request log probabilities (number of tokens) |
| Flag | Description |
|---|---|
-m, --model <MODEL> |
Model ID to use (e.g., claude-3-opus) |
--spec-model <MODEL> |
Model for specification mode |
--use-spec |
Start in specification mode |
-r, --reasoning-effort <LEVEL> |
Reasoning effort level |
--max-tokens <N> |
Maximum tokens in response |
| Flag | Description |
|---|---|
--auto <LEVEL> |
Autonomy level: read-only (default), low, medium, high |
--skip-permissions-unsafe |
Skip ALL permission checks (DANGEROUS) |
--max-turns <N> |
Maximum turns (default: 100) |
--timeout <SECS> |
Timeout in seconds (default: 600, 0 for unlimited) |
--cwd <PATH> |
Working directory for execution |
| Flag | Description |
|---|---|
--enabled-tools <TOOLS> |
Enable specific tools (comma-separated) |
--disabled-tools <TOOLS> |
Disable specific tools (comma-separated) |
--list-tools |
List available tools and exit |
| Flag | Description |
|---|---|
-s, --session-id <ID> |
Continue an existing session |
--user <ID> |
User identifier for tracking |
| Flag | Description |
|---|---|
-v, --verbose |
Enable verbose output |
--system <PROMPT> |
Custom system prompt |
--suffix <TEXT> |
Suffix for completion mode |
--frequency-penalty <N> |
Frequency penalty (-2.0 to 2.0) |
--presence-penalty <N> |
Presence penalty (-2.0 to 2.0) |
--stop <SEQ> |
Stop sequence (can be used multiple times) |
-n, --n <N> |
Number of completions to generate |
--best-of <N> |
Generate best_of completions and return the best |
The --auto flag controls what actions Cortex can perform without explicit approval.
| Level | Description | Allowed Operations |
|---|---|---|
read-only |
No modifications (default) | Read files, search, analyze |
low |
Basic file operations | Read, write files, formatting |
medium |
Development operations | Package install, builds, local git |
high |
Full access | Everything including git push, deployments |
read-only (safest)
- Code review and analysis
- Documentation generation (read-only)
- Architecture exploration
- Planning and estimation
cortex exec --auto read-only "Analyze this codebase for security vulnerabilities"low
- Documentation updates
- Code formatting
- Simple file modifications
- Configuration updates
cortex exec --auto low "Fix all formatting issues in src/"medium
- Feature implementation
- Bug fixes
- Test writing
- Local git operations (commit, branch)
cortex exec --auto medium "Implement unit tests for the auth module"high
- CI/CD deployments
- Remote git operations (push, merge)
- Database migrations
- Production operations
cortex exec --auto high "Deploy to staging and run integration tests"
⚠️ EXTREME CAUTION REQUIRED⚠️ The
--skip-permissions-unsafeflag is inherently dangerous and should be avoided in almost all cases. Using this flag can lead to:
- Unintended file deletions or modifications
- Exposure of sensitive data
- System-wide changes that are difficult to reverse
- Security vulnerabilities in your environment
The --skip-permissions-unsafe flag bypasses ALL permission checks:
# ⚠️ DANGEROUS: Use only in fully isolated, ephemeral environments
# Never use this on production systems or with sensitive data
cortex exec --skip-permissions-unsafe "full system access task"When is this acceptable?
- Isolated Docker containers that are discarded after use
- Ephemeral CI/CD runners with no sensitive data
- Sandboxed testing environments
When should you NEVER use this?
- Production systems
- Any environment with sensitive data or credentials
- Shared development machines
- When processing untrusted input
Plain text output suitable for human reading:
cortex exec "explain this function"Structured JSON output for programmatic consumption:
cortex exec -o json "list all TODO comments" | jq '.response'Output structure:
{
"response": "The AI's response text",
"tool_calls": [...],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 50
}
}Newline-delimited JSON for streaming and log processing:
cortex exec -o jsonl "analyze code"Markdown-formatted output:
cortex exec -o markdown "document this API" > api-docs.mdDefault: 600 seconds (10 minutes)
# Set custom timeout
cortex exec --timeout 1800 "long running task"
# No timeout (be careful!)
cortex exec --timeout 0 "indefinite task"Default: 100 turns
# Limit to 10 turns
cortex exec --max-turns 10 "quick task"A "turn" is one complete request-response cycle with the AI model.
name: Code Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Cortex Review
run: |
cortex exec --auto read-only \
--git-diff \
"Review this PR for bugs, security issues, and code quality"code-review:
script:
- cortex exec --auto read-only -o json "Review the code changes" > review.json
artifacts:
paths:
- review.json#!/bin/bash
set -e
# Fix all linting errors
cortex exec --auto low \
--timeout 300 \
"Fix all ESLint errors in the codebase, commit the changes"#!/bin/bash
# Process multiple files
for file in src/*.rs; do
# Quote variable to prevent word splitting and glob expansion
cortex exec --auto low \
"Add documentation comments to all public functions in \"$file\""
done#!/bin/bash
# Only run if there are changes
if git diff --quiet; then
echo "No changes to review"
else
cortex exec --auto read-only --git-diff \
"Review these changes for issues"
ficortex exec --output-schema '{"type": "object", "properties": {"bugs": {"type": "array"}}}' \
"List all bugs found as a JSON array"# Extract specific fields
cortex exec -o json "list dependencies" | jq -r '.response'
# Count items
cortex exec -o json "list TODO comments" | jq '.tool_calls | length'# Start a session
SESSION=$(cortex exec -o json "analyze codebase" | jq -r '.session_id')
# Continue the session (quote variable to handle edge cases)
cortex exec -s "$SESSION" "now focus on the auth module"# Include specific files
cortex exec --include "src/main.rs" --include "Cargo.toml" \
"Explain how this application works"
# Include patterns
cortex exec --include "src/**/*.rs" --exclude "**/*_test.rs" \
"Review the source code"cortex exec --url "https://api.example.com/docs" \
"How do I authenticate with this API?"cortex exec --git-diff "Review my uncommitted changes"# Use a specific model
cortex exec -m claude-3-opus "complex reasoning task"
# Use different models for different tasks
cortex exec -m gpt-4-turbo --max-tokens 4000 "generate documentation"# Analyze an image
cortex exec -i screenshot.png "What's shown in this screenshot?"
# Multiple images
cortex exec -i img1.png -i img2.png "Compare these two diagrams"Start with read-only and only increase autonomy when necessary:
# Good: Minimal permissions
cortex exec --auto read-only "review code"
# Good: Appropriate for the task
cortex exec --auto medium "implement feature"
# Bad: Excessive permissions
cortex exec --auto high "read a file"Prevent runaway execution with appropriate timeouts:
# Quick tasks
cortex exec --timeout 60 "format this file"
# Complex tasks
cortex exec --timeout 1800 "refactor entire module"Parse results programmatically with JSON output:
result=$(cortex exec -o json "analyze code")
status=$(echo "$result" | jq -r '.status')Test in lower environments first with proper safeguards:
# Test in staging with timeout and turn limits
cortex exec --auto medium --cwd /staging \
--timeout 300 --max-turns 20 \
"test changes"
# Production deployments should include:
# - Explicit timeouts to prevent runaway execution
# - Turn limits for predictable behavior
# - Logging for audit trails
# - Dry-run verification when possible
cortex exec --auto high --cwd /production \
--timeout 600 --max-turns 50 \
-o jsonl "deploy" 2>&1 | tee deploy-$(date +%Y%m%d-%H%M%S).logProduction Safety Checklist:
- Run dry-run or staging tests first
- Set explicit
--timeoutvalues - Set explicit
--max-turnslimits - Enable logging with
-o jsonlandtee - Have rollback procedures ready
- Monitor execution in real-time when possible
Capture output for debugging:
cortex exec -o jsonl "task" 2>&1 | tee execution.logIncrease the timeout or reduce task scope:
# Increase timeout
cortex exec --timeout 3600 "long task"
# Or break into smaller tasks
cortex exec --timeout 300 "first part"
cortex exec --timeout 300 "second part"Check the autonomy level:
# Upgrade autonomy level
cortex exec --auto medium "task requiring writes"Use verbose mode to debug:
cortex exec -v "task"Sessions may expire. Start a new session if continuation fails:
cortex exec "start fresh with the task"