> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/opencode-ai/opencode/llms.txt
> Use this file to discover all available pages before exploring further.

# Non-interactive mode

> Run OpenCode with command-line prompts for scripting and automation

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:

```bash theme={null}
opencode -p "Explain the use of context in Go"
```

The AI's response is printed to standard output, and OpenCode exits when complete.

## Output formats

Control the output format using the `-f` or `--output-format` flag:

### Text format (default)

Plain text output is the default format:

```bash theme={null}
opencode -p "List the files in this project"
```

Output:

```
Here are the files in this project:

- main.go
- README.md
- go.mod
...
```

### JSON format

Wrap the output in a JSON object for easier parsing:

```bash theme={null}
opencode -p "Explain the use of context in Go" -f json
```

Output:

```json theme={null}
{
  "response": "Context in Go is used for..."
}
```

<Tip>
  Use JSON format when integrating OpenCode into scripts or CI/CD pipelines where you need to parse the output programmatically.
</Tip>

## Quiet mode

By default, OpenCode displays a spinner animation while processing your query. Suppress the spinner with the `-q` or `--quiet` flag:

```bash theme={null}
opencode -p "Refactor this function" -q
```

<Note>
  Quiet mode is particularly useful when piping output to other commands or running in automated environments where terminal animations may cause issues.
</Note>

## Combining flags

You can combine multiple flags for maximum control:

```bash theme={null}
# 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

<Warning>
  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.
</Warning>

## 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`

```bash theme={null}
# 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:

```bash theme={null}
opencode -p "What does this function do?" -q
```

### Automated code review

Integrate OpenCode into CI/CD pipelines:

```bash theme={null}
#!/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:

```bash theme={null}
# 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:

```bash theme={null}
#!/bin/bash
# .git/hooks/pre-commit

changed_files=$(git diff --cached --name-only)
opencode -p "Review these changes for common issues: $changed_files" -q
```

<Tip>
  Combine non-interactive mode with shell functions or aliases for frequently used commands:

  ```bash theme={null}
  alias ai-review='opencode -p "Review current changes" -q'
  alias ai-explain='opencode -p'
  ```
</Tip>

## Error handling

Non-interactive mode returns appropriate exit codes:

* `0`: Success
* `1`: Error occurred (configuration issue, network error, etc.)

```bash theme={null}
if opencode -p "Fix this bug" -q; then
  echo "AI completed successfully"
else
  echo "AI encountered an error"
  exit 1
fi
```

## Limitations

<Warning>
  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
</Warning>

For complex tasks requiring back-and-forth conversation, use interactive mode instead:

```bash theme={null}
# 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

```json theme={null}
{
  "agents": {
    "coder": {
      "model": "claude-3.7-sonnet",
      "maxTokens": 5000
    }
  }
}
```

## Examples

### Generate commit messages

```bash theme={null}
diff=$(git diff --cached)
opencode -p "Generate a commit message for: $diff" -q
```

### Code refactoring suggestions

```bash theme={null}
opencode -p "Suggest refactorings for files changed in the last commit" -f json
```

### Documentation generation

```bash theme={null}
for file in src/**/*.go; do
  echo "Processing $file..."
  opencode -p "Add docstrings to $file" -q
done
```

### Security analysis

```bash theme={null}
opencode -p "Scan for security vulnerabilities in this codebase" -f json -q | \
  jq -r '.response' > security-report.md
```
