Pi Agent: Non-Interactive Mode
Last updated: 2026-08-31
Interactive mode is for exploration; non-interactive mode is for production — unattended, batch processing, pipeline orchestration.
1. What Is Non-Interactive Mode
Non-interactive mode means the Agent receives input and executes automatically without human intervention. Suitable for:
- Automated tasks in CI/CD pipelines
- Scheduled batch jobs
- Pipe operations (combined with other CLI tools)
- Unattended script integration
2. CLI Non-Interactive Usage
(1) Single Execution
BASH
pi-agent run "Explain what this code does: def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)"
(2) Read Input from File
BASH
pi-agent run --input code.py "Review the code quality of this file"
(3) Pipe from Standard Input
BASH
cat error.log | pi-agent run "Analyze these error logs and find the most common issues"
(4) Output to File
BASH
pi-agent run --input data.csv "Analyze data trends" --output report.md
3. Python Batch Processing
(1) Basic Batch
PYTHON
from pi_agent import Agent
agent = Agent(name="reviewer", system_prompt="You are a code review expert")
files = ["main.py", "utils.py", "config.py"]
for f in files:
result = agent.run(f"Review code quality of {f}, give scores and improvement suggestions")
print(f"=== {f} ===")
print(result)
print()
(2) Parallel Batch
PYTHON
import asyncio
from pi_agent import AsyncAgent
async def review_file(filename):
agent = AsyncAgent(name="reviewer")
result = await agent.run(f"Review {filename}")
return filename, result
async def main():
files = ["main.py", "utils.py", "config.py", "tests.py"]
tasks = [review_file(f) for f in files]
results = await asyncio.gather(*tasks)
for filename, result in results:
print(f"=== {filename} ===")
print(result)
asyncio.run(main())
4. Pipes & Script Integration
Example 1: Git Commit Message Generator (Difficulty: ⭐⭐)
BASH
git diff --staged | pi-agent run "Generate a concise git commit message based on the code changes"
Example 2: Log Analysis Pipeline (Difficulty: ⭐⭐)
BASH
tail -100 /var/log/app.log | pi-agent run --skill log_analyzer "Extract errors and warnings, sort by frequency"
Example 3: Automated Test Report (Difficulty: ⭐⭐⭐)
PYTHON
from pi_agent import Agent
import subprocess
agent = Agent(name="qa", system_prompt="You are a QA engineer, analyze test results and generate reports")
result = subprocess.run(["pytest", "--tb=short"], capture_output=True, text=True)
report = agent.run(f"Analyze the following test results and generate a report:\n{result.stdout}\n{result.stderr}")
with open("test_report.md", "w") as f:
f.write(report.text)
5. Exit Codes & Error Handling
| Exit Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Configuration error |
| 3 | API call failed |
| 4 | Tool execution error |
BASH
pi-agent run "Check deployment status" --skill devops
if [ $? -eq 0 ]; then
echo "Check passed"
else
echo "Check failed, exit code: $?"
fi
Python:
PYTHON
from pi_agent import Agent, AgentError
agent = Agent(name="checker")
try:
result = agent.run("Verify configuration file")
print("Verification passed:", result.text)
except AgentError as e:
print(f"Execution failed: {e}")
print(f"Error code: {e.code}")
6. Scheduled Task Integration
BASH
# crontab -e
0 9 * * * pi-agent run --skill daily_report "Generate today's project progress report" --output /reports/daily.md
0 0 * * 0 pi-agent run --skill code_review "Review this week's code changes" --output /reports/weekly.md
FAQ
Q Does non-interactive mode support streaming?
A Not by default, but you can use the
--stream flag. However, piping usually doesn't need streaming.Q Is there a pipe input length limit?
A No hard limit, but constrained by the model's context window. Very long input is automatically truncated or chunked.
Q What about API rate limits during batch processing?
A Pi Agent has built-in retry. You can also add
asyncio.sleep() in code to control request frequency.Summary
- Non-interactive mode suits CI/CD, batch processing, pipe operations
- CLI:
pi-agent run; Python:agent.run() - Supports stdin pipe, file input, file output
- Exit codes distinguish error types for script integration
- Integrates with cron and CI systems for scheduled automation
Exercises
- Basic (Difficulty: ⭐): Execute a single task with
pi-agent runand save results to a file. - Intermediate (Difficulty: ⭐⭐): Write a batch script to review all Python files in a directory.
- Advanced (Difficulty: ⭐⭐⭐): Set up a git hook that auto-generates commit messages with Pi Agent before committing.