DEV Community

I Built an App to Run Local LLMs on Colab and Vast.ai in One Night

Introduction

I wanted to try local LLMs such as Qwen, but the GPU in my own PC is too weak to run them. Renting a GPU on Colab or Vast.ai works, but redoing the setup every time is tedious.

So, together with Claude, I built a harness (a tool) for running "local LLMs" on rented GPUs in one night, and turned it into an app. It is called llm-harness.

This article is for you if:

  • Your PC's GPU is weak, but you want to try local LLMs (such as Qwen)
  • You have used Colab or Vast.ai, but find the setup tedious every time
  • You want a cheap way to provide an LLM behind your AI agents

The part I struggled with most was working within Colab's rules, so that is the focus of this article.

What I Built

llm-harness runs one of vLLM, llama.cpp, or Ollama on a Colab or Vast.ai GPU.

  • Whichever one you use, it becomes an "OpenAI-compatible API", so you can use it directly from the OpenAI SDK, LangChain, or your own agent
  • It works both as a command-line tool (CLI) and as an app (a UI)
  • The app starts with a double-click and opens in the browser
  • You can "find a GPU → start → chat → stop" with buttons
  • It shows the status, elapsed time, and the cost so far
  • You can put a shortcut (a green speech-bubble icon) on the desktop

What I Used

  • Python (mostly the standard library; the only dependency is pyyaml, plus vastai / google-colab-cli only for those who use them)
  • vLLM / llama.cpp / Ollama (inference servers)
  • The official Vast.ai CLI (vastai)
  • Google's official Colab CLI (google-colab-cli)
  • A single HTML file with plain JavaScript for the UI (no framework)
  • pytest and browser automation (Playwright) for testing

How It Came Together

I shaped it step by step while asking Claude for changes.

  • I first built it as a CLI tool, with commands such as llm-harness up / status / chat / down
  • I asked for an app, so a UI that opens in the browser was added
  • I wanted to run it once for real, so I ran it on Colab (T4), and it worked
  • I wanted to run Colab from inside the app, so I integrated Google's official Colab CLI
  • Finally, I asked for a shortcut on my PC, so a bat file for creating the shortcut and an icon were added

How It Works

There are two routes for renting a GPU: Vast.ai and Colab.

  • Vast.ai route : find a GPU and start an instance with the official CLI (vastai), then start an inference server
  • Colab route : rent a GPU runtime with Google's official Colab CLI, then start an inference server inside it

Both routes end up usable as an OpenAI-compatible API. The Colab route is built a bit differently because of Colab's rules, which I cover in the next section.

Switching the Inference Engine Automatically by GPU

The harness looks at the generation of the rented GPU and picks the inference engine automatically.

  • T4 (older generation, compute capability 7.5) → Ollama
  • L4 / A100 and similar (8.0 or higher) → vLLM

There are three reasons for choosing Ollama on the T4:

  • Building llama.cpp from source on Colab looked like it would take more than 20 minutes, because there are only 2 CPU cores
  • Ollama ships prebuilt, and installing it takes 1-2 minutes
  • The server is not exposed to the outside, so it is fine that Ollama has no API key feature

App Security

The app runs only on 127.0.0.1 on my own PC. On top of that, I added two protections so that other websites cannot operate it without permission:

  • Check the Host header
  • Require a custom header (X-Harness) on POST requests

When another website tries to send a request with a custom header, the browser blocks it. The app relies on that behavior.

Colab's Rules, and Solving Them with the Colab CLI

This is the highlight of the article.

The First Idea Was a Tunnel

The first idea was to "expose the server started on Colab through a tunnel (cloudflared or ngrok) and use it from my PC." However, Colab's FAQ prohibits offering web services to the outside and "connecting to remote proxies." On top of that, on the free tier, bypassing the notebook UI to work through a web UI is also prohibited.

Reference: Colab FAQ (Which activities are restricted in Colab?)

So I decided not to use a tunnel.

First Solution: Use It Only Inside the Notebook

The first solution was to run the server only inside Colab (127.0.0.1) and use it from within the same notebook. Nothing is exposed to the outside.

But with this, I could not use it from the app on my PC.

Final Solution: colab exec in the Colab CLI

In the end, I used Google's official Colab CLI. The app sends code with colab exec and receives the result. This let me chat from the app on my own PC without exposing the server.

The flow looks like this:

# Rent a GPU runtime
colab new -s llm-harness --gpu T4
# Send the startup script and start the server
colab exec -s llm-harness -f setup.py
# For each chat message, send a small script with colab exec and call the LLM from inside the runtime
# Return the runtime
colab stop -s llm-harness

This works thanks to two properties described in the official documentation:

  • Python state (variables and so on) persists across exec calls → a server started once can be reused
  • Output streams in real time → chat replies can be shown character by character

I passed the chat content through environment variables (--env) instead of embedding it in the code. When I checked the Colab CLI source, the values were safely embedded using Python's repr.

The GPU options are T4 / L4 / G4 / A100 / H100. Which ones you can use depends on your plan.

The Lesson I learned that it matters to design within the rules, rather than settling for "it works, so it's fine."

Results on a T4, and the Warm-Up Trick

The Results

I actually ran it on a T4 with Colab Pro. The GPU check came out as follows, and since it was a T4, Ollama was selected:

GPU: Tesla T4 15360 MiB compute 7.5 → backend: ollama

The model was qwen3:8b.

  • "What is the capital of Japan?" → "The capital of Japan is Tokyo."
  • "Explain quantum computers in three lines" → a three-line explanation using qubits, superposition, and entanglement streamed out (I asked both questions in Japanese; the above are translations.)

Two Questions Took 174.9 Seconds

However, the two questions took 174.9 seconds. It looked like it had stalled partway, so I checked nvidia-smi in a terminal and saw about 5.4 GB loaded into GPU memory. The slowness was because the model was being loaded onto the GPU on the first request only.

Fixing It with a Warm-Up

So I added two things:

  • Send one "dummy" request at startup to load the model in advance (warm-up)
  • Keep the model on the GPU with OLLAMA_KEEP_ALIVE=-1

With this, replies come right away once "Ready" is shown.

Preventing Billing Accidents

The scary part about rented GPUs is getting billed because you forgot to stop one. I built the following into the Vast.ai route:

  • Before starting, it shows a confirmation screen with "the hourly price, the time until auto-stop, and the maximum it can cost"
  • Once the set time passes (3 hours by default), the instance deletes itself
  • For this, it uses an instance-only key (CONTAINER_API_KEY). My main API key is never passed to the instance
  • When I press "Stop", it also checks whether any forgotten instances are still left and warns me
  • Even if startup fails partway, the information is saved first, so "Stop" can still delete the instance

The API key is generated randomly every time it starts.

Testing Tricks

A Fake CLI and a Fake LLM Server

Using the real Vast.ai or Colab costs money. So I built a "fake CLI" and a "fake LLM server" for testing. I wrote fake vastai and fake colab commands in Python and swapped them in for the real ones with environment variables.

End-to-End Testing with Browser Automation

With browser automation (Playwright), the tests actually click through the UI and check "GPU search → start → chat → stop" end to end. These tests found a small bug where the text on the send button was wrapping vertically, and I fixed it.

What I Haven't Tried Yet, and What's Next

What I Haven't Tried Yet

To be honest, I have not tried the following two things for real yet:

  • Starting on Vast.ai in production : I confirmed the option names with the real CLI, and the tests with the fake CLI pass. But I have not started a real instance yet
  • Starting via the Colab CLI from the app : the tests with the fake CLI pass, and I checked the Colab CLI source. But I have not confirmed it on real Colab yet

That said, running it in the notebook version (Colab in the browser) has worked. That is the T4 result from the previous section.

What I Want to Do Next

  • Test it for real on Vast.ai
  • Connect it as the backend for my own agents (such as Omoikane)
  • Publish it on GitHub (koheip)

Summary

  • llm-harness runs one of vLLM, llama.cpp, or Ollama on rented Colab or Vast.ai GPUs and makes it usable as an OpenAI-compatible API
  • On Colab, I did not use a tunnel because that goes against the rules. Instead, I used colab exec from Google's official Colab CLI to chat from the app without exposing the server
  • On a T4, it picks Ollama automatically, and a warm-up plus OLLAMA_KEEP_ALIVE=-1 removed the initial wait
  • To prevent billing accidents, I added a pre-start confirmation, automatic deletion, and a warning about forgotten instances
  • To test without spending money, I used a fake CLI, a fake LLM server, and Playwright
  • Design within the rules, rather than settling for "it works, so it's fine." That was the biggest lesson for me this time.

Bonus: A Tip for Handing Work to AI Agents

I handed the job of putting a shortcut on my PC to another agent (grokbot). Instead of my instructions for this task, grokbot read a different instruction file I had written on 11/17 (one for saving articles as drafts on Zenn and Qiita). When handing work to an agent, pasting the text itself is more reliable than giving it a file name.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.