Back to articles
AI & Cloud

Build an AI Agent for Automated Code Auditing Running on VPS with LangGraph and Local LLM

This guide shows how to build an AI agent that automatically audits source code on a VPS using LangGraph and a Local LLM, enabling secure, offline code quality checks for...

DPTCloud Engineering6 min read
Build an AI Agent for Automated Code Auditing Running on VPS with LangGraph and Local LLM

This guide shows how to build an AI agent that automatically audits source code on a VPS using LangGraph and a Local LLM, enabling secure, offline code quality checks for high-security enterprise environments.

Introduction and Objectives

In modern software development, code auditing is essential to ensure quality, security, and performance. However, manual auditing is time-consuming and prone to oversight. With AI advancements, we can now build an AI agent to automatically analyze code, detect issues, and suggest optimizations—all running locally on your VPS using LangGraph and a Local LLM. This guide walks through environment setup, agent design, deployment, and validation, enabling fully offline, secure code auditing for enterprises with strict data control requirements.

Requirements and Setup

To follow this guide, you need:

  • A VPS (Cloud VPS or Dedicated Server) with high RAM (at least 16GB, preferably 32GB or more) to run the Local LLM.
  • Linux operating system (Ubuntu 22.04 or similar).
  • Python 3.10 or higher.
  • Docker and Docker Compose installed (if using containerized deployment).
  • A pre-downloaded Local LLM (e.g., Llama 3, Phi-3, or Mistral 7B).
  • LangGraph, LangChain, GitPython, Pydantic, and related libraries.

We’ll use a high-RAM VPS to host the Local LLM, as large models require substantial memory. You can choose from High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways or Dedicated Compute Servers for AI & Inference Workloads.

Building the AI Agent with LangGraph and Local LLM

LangGraph enables complex, multi-step AI systems where each step can be a model, tool, or conditional logic. Combined with a Local LLM, this allows full control and security without internet dependency.

First, install the required libraries:

pip install langchain langchain-community langchain-core langgraph langchain-openai langchain-textsplitters gitpython pydantic

The agent will perform the following tasks:

  • Fetch source code from a repository (e.g., GitHub).
  • Analyze code based on security, performance, and coding standards.
  • Detect potential bugs and suggest fixes.
  • Generate reports in text or HTML format.

Here’s a basic agent structure:

from langgraph.graph import StateGraph, MessagesState
from langchain_core.messages import HumanMessage
from langchain_core.output parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.llms import HuggingFaceHub
from langchain_community.tools import Tool
from langchain_core.runnables import RunnableLambda
from langchain_community.document_loaders import GitLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

# Initialize local LLM
hf = HuggingFaceHub(repo_id="meta-llama/Llama-3-8b", huggingfacehub_api_token="your_token")

# Load code from GitHub
loader = GitLoader(
    repo_url="https://github.com/your-repo",
    branch="main",
    subdirectory="src",
)
documents = loader.load()

# Split documents into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
docs = text_splitter.split_documents(documents)

# Define prompt for code auditing
prompt = ChatPromptTemplate.from_template(
    "You are a professional code auditor. Analyze the following source code and evaluate: 1. Are there security vulnerabilities? 2. Does it violate coding standards? 3. Can performance be optimized? Source code: {text}"
)

# Create chain
chain = prompt | hf | StrOutputParser()

# Run audit
result = chain.invoke({"text": docs[0].page_content})
print(result)

This agent fetches code from GitHub, splits it into manageable chunks, and sends it to the Local LLM for analysis. The output is a detailed audit report.

Deployment and Background Execution on VPS

To run the agent in the background on the VPS, use systemd or Docker for automatic startup.

Here’s a systemd service file example:

[Unit]
Description=Code Auditor Agent
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /path/to/agent.py
Restart=always
RestartSec=10
User=your_user
WorkingDirectory=/path/to/project

[Install]
WantedBy=multi-user.target

Alternatively, use Docker Compose for containerized deployment:

version: '3.8'
services:
  code-auditor:
    image: python:3.10
    working_dir: /app
    volumes:
      - ./app:/app
    command: python3 agent.py
    environment:
      - HUGGINGFACEHUB_API_TOKEN=your_token

Start the service after boot using:

systemctl enable code-auditor.service
systemctl start code-auditor.service

Check the service status with:

systemctl status code-auditor.service

Testing and Optimization

To validate the agent’s effectiveness:

  • Test with real-world codebases.
  • Compare results with traditional tools like SonarQube or ESLint.
  • Refine prompts for better accuracy.
  • Experiment with different Local LLMs to find the best balance of speed and quality.

The agent can be extended to integrate with CI/CD pipelines like GitHub Actions or GitLab CI for automated code reviews on every commit.

Troubleshooting and Common Issues

Common deployment issues include:

  • Failed code fetch: Check network connectivity and repository access permissions.
  • LLM fails to start: Verify RAM and GPU availability; ensure the model is correctly downloaded.
  • Agent unresponsive: Check logs using journalctl -u code-auditor.service.
  • Slow response: Optimize prompts, use smaller models, or increase VPS RAM.

For further help, refer to similar deployment guides at Automate Livestream Highlight Extraction with FFmpeg and AI Running on Linux VPS or Deploy OpenSign on VPS: Build a Secure, Cost-Optimized Internal Document Signing Platform to Replace DocuSign.

References

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478