Non-interactive mode allows you to run OpenCode with a single prompt from the command line, making it ideal for scripting, automation, and quick one-off queries.
Basic usage
Use the -p or --prompt flag to run a prompt without launching the TUI:
opencode -p "Explain the use of context in Go"
The AI’s response is printed to standard output, and OpenCode exits when complete.
Control the output format using the -f or --output-format flag:
Text format (default)
Plain text output is the default format:
opencode -p "List the files in this project"
Output:
Here are the files in this project:
- main.go
- README.md
- go.mod
...
Wrap the output in a JSON object for easier parsing:
opencode -p "Explain the use of context in Go" -f json
Output:
{
"response": "Context in Go is used for..."
}
Use JSON format when integrating OpenCode into scripts or CI/CD pipelines where you need to parse the output programmatically.
Quiet mode
By default, OpenCode displays a spinner animation while processing your query. Suppress the spinner with the -q or --quiet flag:
opencode -p "Refactor this function" -q
Quiet mode is particularly useful when piping output to other commands or running in automated environments where terminal animations may cause issues.
Combining flags
You can combine multiple flags for maximum control:
# JSON output without spinner
opencode -p "Analyze this codebase" -f json -q
# Run in a specific directory with debug logging
opencode -c /path/to/project -p "Find all TODO comments" -d
Permissions in non-interactive mode
All permissions are automatically approved when running in non-interactive mode:
- The AI can run commands without asking
- File edits are applied automatically
- URLs are fetched without confirmation
Be cautious when running OpenCode in non-interactive mode with prompts that could modify your system or access external resources. Review the prompt carefully before execution.
Session management
Each non-interactive run creates a new session:
- The session title includes “Non-interactive:” followed by the prompt (truncated to 100 characters)
- The session is saved to the database like any other session
- You can view non-interactive sessions later in interactive mode using
Ctrl+S
# This creates a session titled:
# "Non-interactive: Explain the use of context in Go"
opencode -p "Explain the use of context in Go"
Use cases
Quick code explanations
Get instant explanations without launching the full TUI:
opencode -p "What does this function do?" -q
Automated code review
Integrate OpenCode into CI/CD pipelines:
#!/bin/bash
output=$(opencode -p "Review this PR for security issues" -f json -q)
echo "$output" | jq -r '.response'
Script integration
Use OpenCode as part of larger automation workflows:
# Generate documentation
opencode -p "Create API documentation for this module" -q > docs/api.md
# Analyze test coverage
test_results=$(run_tests)
opencode -p "Analyze these test results: $test_results" -f json -q
Git hooks
Add AI assistance to your git workflow:
#!/bin/bash
# .git/hooks/pre-commit
changed_files=$(git diff --cached --name-only)
opencode -p "Review these changes for common issues: $changed_files" -q
Combine non-interactive mode with shell functions or aliases for frequently used commands:alias ai-review='opencode -p "Review current changes" -q'
alias ai-explain='opencode -p'
Error handling
Non-interactive mode returns appropriate exit codes:
0: Success
1: Error occurred (configuration issue, network error, etc.)
if opencode -p "Fix this bug" -q; then
echo "AI completed successfully"
else
echo "AI encountered an error"
exit 1
fi
Limitations
Non-interactive mode has some limitations compared to interactive mode:
- No session continuity (each run is independent)
- Cannot ask follow-up questions
- No manual permission review
- Limited visibility into AI reasoning process
For complex tasks requiring back-and-forth conversation, use interactive mode instead:
# Start interactive mode for complex tasks
opencode
Configuration
Non-interactive mode respects your configuration file (~/.opencode.json):
- Uses configured AI model and provider
- Applies shell configuration
- Respects LSP settings
- Uses configured data directory
{
"agents": {
"coder": {
"model": "claude-3.7-sonnet",
"maxTokens": 5000
}
}
}
Examples
Generate commit messages
diff=$(git diff --cached)
opencode -p "Generate a commit message for: $diff" -q
Code refactoring suggestions
opencode -p "Suggest refactorings for files changed in the last commit" -f json
Documentation generation
for file in src/**/*.go; do
echo "Processing $file..."
opencode -p "Add docstrings to $file" -q
done
Security analysis
opencode -p "Scan for security vulnerabilities in this codebase" -f json -q | \
jq -r '.response' > security-report.md