GFThe Grown-Ass Field Guide

Home Lab / Local AI in Practice

Autonomous Local Coding: Self-Hosted Orchestration with Ollama, OpenHands, and Smart Routing

Replace recurring cloud API token bills and keep proprietary codebases private by pairing local Ollama runtimes with OpenHands agent workflows and smart routing proxies.

Use Case

Software engineering agents burn millions of tokens per task as they repeatedly read repository structures, modify code, run test suites, and parse error traces. Relying entirely on cloud API endpoints for autonomous coding quickly turns expensive, while transmitting proprietary intellectual property to third-party servers presents compliance hurdles for many development teams. By pairing local model inference (via Ollama) with an autonomous agent harness (like OpenHands) and a smart routing layer (such as 9router or LiteLLM), developers can construct a self-hosted agentic workflow that runs continuous refactoring and testing cycles without token costs or data leakage.

The core architecture connects OpenHands—an open-source platform that enables language models to write code, execute bash commands, and browse documentation—to a locally hosted inference backend. An intermediate routing proxy normalizes API schemas into standard OpenAI-compatible formats, dynamically dispatching tasks based on complexity. Context-heavy, repetitive operations like scanning directory trees, generating unit tests, linting, and boilerplate refactoring stay on local hardware. The agent interacts with your repository inside a local sandbox, creating pull requests or branch patches entirely offline.

Hardware capacity dictates the depth of autonomous capabilities across three primary unified-memory and VRAM tiers:

- **16 GB AI-Memory**: Sufficient for 7B to 8B parameter instruction-tuned coding models (such as Qwen 2.5 Coder 7B or Llama 3.1 8B quantized to 4-bit or 5-bit). Context windows are constrained to roughly 8k–16k tokens before memory exhaustion limits multi-file awareness. This tier works best for targeted single-file fixes, function-level test generation, and structured lint remediation. - **32 GB AI-Memory**: The practical baseline for serious autonomous agent loops. Accommodates 14B to 32B coding models (such as Qwen 2.5 Coder 14B/32B at Q4_K_M) or dense 8B models with expanded 32k context windows. The agent can retain cross-file dependencies and absorb verbose stack traces without degrading into hallucinated syntax. - **64 GB+ AI-Memory**: Unlocks higher-precision 32B models (Q8/FP16) or quantized 70B models (such as Llama 3.3 70B at Q4). This tier supports 64k+ context windows and enables continuous background agent loops without aggressive KV-cache swapping bottlenecks, bringing local output quality significantly closer to proprietary commercial tiers.

While local models excel at well-defined, bounded programming tasks, complex multi-tier architectural decisions, novel algorithmic designs, and obscure edge-case debugging should still route to frontier models (such as Claude 3.5 Sonnet or GPT-4o). The routing proxy should be configured with heuristic fallbacks: local models handle initial drafts and test runs, escalating to a frontier API only when the agent hits a designated failure threshold (such as three unsuccessful test-fix loops) or when a task explicitly demands high-level architectural planning.

The setup burden centers on orchestrating containerized environments and tuning runtime memory limits. Because autonomous agents generate and execute terminal commands, safeguards are mandatory. OpenHands must execute exclusively within an isolated Docker container with read-write access restricted to the targeted project directory. Network egress from the container should be restricted or monitored to prevent unintended external API calls or telemetry transmission. Furthermore, model context limits must be calibrated within Ollama to prevent out-of-memory crashes during iterative tool-use loops.

Deploying this stack delivers operational independence and eliminates token anxiety. Developers can leave agents running automated test generation, documentation updates, and technical debt cleanups overnight on dedicated home lab or workstation hardware without incurring incremental SaaS costs or compromising codebase confidentiality.

Practical outline

What it takes to make this useful.

  1. 01

    Run OpenHands inside a strictly sandboxed Docker container with filesystem mounts scoped only to the target repository.

  2. 02

    Deploy Ollama with specialized code generation models sized to your memory tier (e.g., Qwen 2.5 Coder 7B on 16 GB, 32B on 32–64 GB).

  3. 03

    Use a local routing proxy (such as 9router or LiteLLM) to standardize API calls, handle retries, and manage fallback rules.

  4. 04

    Implement an escalation policy that defaults to local inference and only offloads to frontier APIs after repeated agent test failures.

  5. 05

    Restrict network egress on agent execution containers to prevent accidental data leakage or unmonitored external calls.

  6. 06

    Monitor VRAM allocation and set explicit context window caps in Ollama to prevent out-of-memory termination during long-running agent loops.

Source trail

Evidence and examples behind the guide.