Automating with Ollama
What this article covers
- How to integrate Ollama into scripts and workflows.
- Practical automation use cases.
- Examples in Bash and Python.
- Tips for reliability, logging, and scheduling.
- Security and error handling.
Introduction: Automating with Ollama
Ollama isn’t just for interactive chats. You can use it to automate repetitive tasks like text classification, summarization, notifications, data analysis, and document processing. With scripts, cron jobs, or workflow tools, you can handle recurring tasks more efficiently while keeping your data local and private.
This article walks you through integrating Ollama into your automation workflows and shows which use cases work best.
Key concepts
- Automation: Tasks execute without manual intervention.
- Cron job: Time-based execution of a script.
- Script: An executable file containing commands.
- Workflow: A sequence of multiple tasks.
- Pipeline: A chain of processing steps.
- Batch: Processing multiple items in one run.
- Log: A record of execution.
- Trigger: An event that starts the automation.
The basic approach
Ollama exposes a simple API that scripts can call:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.1",
"prompt": "Summarize this text: ...",
"stream": false
}'
The response can then be processed, saved, or forwarded.
Example: Email classification
#!/bin/bash
email_text=$(cat email.txt)
antwort=$(curl -s http://localhost:11434/api/generate -d "{
\"model\": \"llama3.1\",
\"prompt\": \"Classify this email as SPAM, IMPORTANT, or INFO: $email_text\",
\"stream\": false
}")
echo "$antwort" | jq -r '.response'
Example: Summarizing a document
#!/bin/bash
text=$(cat dokument.txt)
zusammenfassung=$(curl -s http://localhost:11434/api/generate -d "{
\"model\": \"llama3.1\",
\"prompt\": \"Summarize the following text in three sentences: $text\",
\"stream\": false
}")
echo "$zusammenfassung" | jq -r '.response' > summary.txt
Example: Python script
import requests
import json
def classify(text, model="llama3.1"):
url = "http://localhost:11434/api/generate"
payload = {
"model": model,
"prompt": f"Classify the following text: {text}",
"stream": False
}
response = requests.post(url, json=payload)
return response.json()["response"]
with open("documents.txt") as f:
content = f.read()
result = classify(content)
print(result)
Cron jobs for scheduled tasks
0 8 * * * /usr/local/bin/ollama-summary.sh >> /var/log/ollama-cron.log 2>&1
Workflow tools
- n8n: Visual workflows with Ollama via HTTP requests.
- Home Assistant: Automations triggered by voice commands.
- Node-RED: Flow-based data automation.
- GitHub Actions / GitLab CI: Automated AI checks for your repositories.
- Airflow: Complex data pipelines.
Practical automation ideas
- Classify emails or support tickets.
- Generate document summaries.
- Auto-reply to chat messages.
- Rename or move files based on content.
- Analyze log files.
- Create alerts and notifications.
- Add code comments.
- Extract data from PDFs.
- Generate social media copy.
- Categorize content.
Logging and error handling
if curl -f http://localhost:11434/api/tags; then
echo "Ollama is reachable"
else
echo "Ollama is not reachable" >&2
exit 1
fi
In Python:
try:
result = classify(text)
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
exit(1)
Scheduling strategies
- Daily for recurring reports.
- Weekly for summaries.
- On file changes or events.
- At regular intervals.
Security
- Restrict API access to localhost.
- Avoid placing sensitive data in prompts without protection.
- Validate inputs to prevent prompt injection.
- Review outputs before they’re automatically forwarded.
- Check logs for sensitive content.
Best practices
- Make scripts idempotent.
- Log errors clearly.
- Limit resource consumption.
- Choose models suitable for your task.
- Start simple and expand incrementally.
Common pitfalls
- Ollama isn’t running: Script fails silently.
- Model not found: Wrong name or model not pulled.
- Encoding issues: Special characters or diacritics mishandled.
- Timeout: Response takes too long.
- No validation: Bad output gets processed anyway.
- Missing logs: Errors are hard to trace.
Further reading
- BotServ.de Ollama Commands
- BotServ.de Ollama API Libraries
- BotServ.de Ollama REST API
- BotServ.de Ollama Integrations
FAQ: Automating with Ollama
Can Ollama classify emails? Yes, a simple prompt-based script can categorize text.
How often can I call Ollama? As often as your hardware and model cache allow.
Do I need Docker to automate with Ollama? No, Ollama runs natively or in containers.
How do I handle connection failures? Check connectivity first and use proper exit codes.
Can I use Ollama in n8n? Yes, through HTTP request nodes.
References
- Ollama API: https://github.com/ollama/ollama/blob/main/docs/api.md
- n8n: https://n8n.io/
- cron: https://linux.die.net/man/8/cron
Summary: Automating with Ollama
Ollama integrates easily into scripts, cron jobs, and workflow tools. You can classify text, summarize documents, and process data entirely on your machine. Solid error handling, good logging, and the right model choice are essential. By building simple, repeatable pipelines, you can automate routine tasks with local AI without sending your data to external services.


