DEV Community

shailendra khade
shailendra khade

Posted on

Autonomous DevOps AI Agent (FastAPI + Ollama)

Building an Autonomous DevOps AI Agent using FastAPI and Ollama
Introduction

DevOps is no longer just about CI/CD pipelines and monitoring dashboards.
With the rise of Large Language Models (LLMs), DevOps is evolving into AI-powered automation (AIOps).

In this project, I built an Autonomous DevOps AI Agent using:

  • Ollama (Local LLM – Mistral)
  • FastAPI (API Layer)
  • Linux System Tools
  • Git & Deployment Scripts

This AI agent can analyze system metrics, interact with Git repositories, trigger deployments, and reason about DevOps tasks using an LLM.

This post explains the architecture, implementation, challenges, and learnings.


What is an Autonomous DevOps AI Agent?

  • An Autonomous DevOps AI Agent is a system that:
  • Collects infrastructure data (CPU, RAM, logs, etc.)
  • Executes DevOps operations (Git, scripts, deployments)
  • Uses AI (LLM) to analyze and respond intelligently
  • Exposes everything via REST APIs

In simple words:

AI + DevOps + Automation = Autonomous Agent

Architecture

Components:

  • FastAPI – API Gateway and Controller
  • Ollama – Local LLM runtime (Mistral model)
  • Linux Tools – System metrics and logs
  • Git & Shell Scripts – DevOps automation

Step 1: Install Ollama and Model

Install Ollama on Linux:

curl -fsSL https://ollama.com/install.sh | sh

Pull an LLM model:

ollama pull mistral

Verify:

ollama list


Step 2: Setup Python Environment

Create virtual environment:

python3 -m venv ai-env
source ai-env/bin/activate

Install dependencies:

pip install fastapi uvicorn requests psutil


Step 3: Project Structure
devops-ai-agent/

├── agent.py # AI reasoning (Ollama)
├── system.py # System metrics
├── git_ops.py # Git automation
├── deploy.py # Deployment scripts
├── main.py # FastAPI entry point


Step 4: Core Implementation
system.py – System Metrics

import psutil

def get_system_info():
    return {
        "cpu": psutil.cpu_percent(),
        "memory": psutil.virtual_memory().percent,
        "disk": psutil.disk_usage("/").percent
    }
Enter fullscreen mode Exit fullscreen mode

agent.py – AI Brain (Ollama Integration)

import requests

OLLAMA_URL = "http://localhost:11434/api/generate"

def ai_think(prompt):
    payload = {
        "model": "mistral",
        "prompt": prompt,
        "stream": False
    }
    response = requests.post(OLLAMA_URL, json=payload)
    return response.json()["response"]
Enter fullscreen mode Exit fullscreen mode

git_ops.py – Git Automation

import os

def clone_repo(repo_url):
    os.system(f"git clone {repo_url}")
    return {"status": "repo cloned", "repo": repo_url}
Enter fullscreen mode Exit fullscreen mode

deploy.py – Deployment Trigger

import os

def deploy_app():
    os.system("echo Deploying application...")
    return {"status": "deployment triggered"}
Enter fullscreen mode Exit fullscreen mode

main.py – FastAPI Autonomous Agent

from fastapi import FastAPI
from system import get_system_info
from git_ops import clone_repo
from deploy import deploy_app
from agent import ai_think

app = FastAPI()

@app.get("/")
def home():
    return {"message": "Autonomous DevOps AI Agent Running"}

@app.get("/system")
def system():
    return get_system_info()

@app.post("/chat")
def chat(prompt: str):
    return {"ai_response": ai_think(prompt)}

@app.post("/git/clone")
def git_clone(repo_url: str):
    return clone_repo(repo_url)

@app.post("/deploy")
def deploy():
    return deploy_app()

@app.post("/agent")
def autonomous_agent(task: str):
    reasoning = ai_think(f"You are DevOps AI. Task: {task}")
    return {"task": task, "ai_decision": reasoning}
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the Agent
uvicorn main:app --host 0.0.0.0 --port 7000


Example Use Cases
1 Check System Health
GET /system

2 Ask AI about DevOps
POST /chat?prompt=Explain Kubernetes architecture

3 Clone Git Repository
POST /git/clone?repo_url=https://github.com/user/repo.git

4 Autonomous DevOps Task
POST /agent?task=Check server health and suggest actions



Key Learnings

  • Ollama can act as a local LLM inference server.
  • FastAPI is ideal for building AI-powered microservices.
  • DevOps automation can be enhanced using LLM reasoning.
  • This architecture is similar to real-world AIOps systems.

Top comments (0)