Run open-source LLMs or custom GGUFs on Google Colab using Ollama, llama.cpp, vLLM, SGLang, or HuggingFace Transformers — connected to OpenCode v2 with Configurable Context (32k, 128k, and up to 1M on supported models & hardware), Agent Tools, and Real-Time SSE Streaming.
Before configuring high context lengths or large models, please understand how model architecture limits, GPU VRAM math, and Google Colab's Terms of Service work in practice:
- OpenColab removes Ollama's default 4,096-token software cap by baking
num_ctx(or--max-model-len) into the backend configuration. - However, OpenColab cannot magically give 1M context to a model trained only for 32k or 128k.
- Most current open-weight models (
Qwen 2.5 Coder,DeepSeek-R1,Llama 3.1,Spark-X2.6-4B) have a native ceiling of 128k (131,072) tokens (or 32k–64k for older models). - Setting
CONTEXT_WINDOW = 1048576(1M) only works properly on models explicitly built or extended via YaRN/RoPE for 1M context (such asQwen2.5-7B-Instruct-1MorQwen2.5-14B-Instruct-1M). Forcing 1M context on a 128k-native model will result in garbled output beyond 128k or backend validation errors.
- Most current open-weight models (
Total GPU memory required is Model Weights + KV Cache + Activation Overhead:
- A 32B model (even quantized to 4-bit Q4_K_M) requires ~19–20 GB of VRAM just for the weights alone. It will not fit on a 15 GB Tesla T4 GPU—even at a tiny 4k context! Attempting to load a 32B model on a 15 GB GPU will either crash with
CUDA Out of Memoryor offload layers to slow CPU RAM (<1 token/sec). - 1M Token KV Cache requires massive VRAM: Even with 4-bit KV cache (
q4_0) and Flash Attention, a 1,048,576-token context buffer consumes 15 GB to 40+ GB of extra VRAM depending on model size. - Rule of Thumb:
- 15 GB VRAM (Tesla T4): Stick to 4B – 9B models with 32k – 128k context (or a 14B Q4 model with 16k – 32k context).
- 24 GB VRAM (Nvidia L4): Run 14B – 27B (Q4) models with 64k – 128k context.
- 40 GB – 80 GB VRAM (Nvidia A100): Required for 27B – 70B models at 128k – 256k context, or 7B – 14B 1M-native models at 1,048,576 context.
- Paid Plan Strongly Required: Running background tunnels (
cloudflared), API servers, or sustained heavy LLM inference on a Google Colab Free account will result in frequent, unannounced runtime terminations and may violate Google Colab's Terms of Service regarding free-tier resource usage and remote proxy hosting. - Use Paid Compute Units (Colab Pro / Pro+ / Pay-As-You-Go): This project is designed for developers utilizing paid Google Colab compute units (Colab Pro / Pro+) who want to use their purchased GPU allocation interactively alongside their IDE. Always keep your Colab notebook tab open and abide by Google's Terms of Service.
- 5 Pluggable Inference Backends: Choose the best engine for your workload via
BACKEND:ollama(Default): Ollama daemon with Flash Attention (OLLAMA_FLASH_ATTENTION=1), 4-bit KV cache (q4_0), and automatic GPU VRAM pre-warming (OLLAMA_KEEP_ALIVE=-1).llamacpp: Nativellama.cppCUDA server (llama-cpp-python[server]) for direct.gguffiles, URLs, or HuggingFace GGUF repos.vllm: High-throughput PagedAttention OpenAI server for Safetensors, AWQ, GPTQ, and FP16/BF16 models with automatic tool-call parsing.sglang: RadixAttention engine for ultra-fast prefix caching and structured agent loops.transformers: Zero-install native Hugging Facetransformers+torchstreaming engine (with optional 4-bitbitsandbytesquantization).
- Bypasses the 4096 Default Cap: Configures custom context limits (
32k,128k,256k, or up to1Mon supported 1M models + A100 GPUs). - Zero Cloudflare 530 Errors: Uses a unified FastAPI proxy server (
:8000) that verifies upstream backend readiness prior to exposing the Cloudflare tunnel. - Universal Reasoning & SSE Normalization: Preserves EventStream framing (
\n\n), normalizes bothreasoning(Ollama) andreasoning_content(vLLM / SGLang / llama.cpp) deltas, and strips backend-incompatible payload fields automatically. - Zero Hardcoded Secrets: Uses cryptographically secure random bearer tokens generated fresh per runtime session.
BACKEND Value |
Engine | Best For | Example MODEL_ID |
|---|---|---|---|
"ollama" (default) |
Ollama | Pre-quantized registry models & local GGUFs | "SparkLLM/Spark-X2.6-4B:q4_k_m", "qwen2.5-coder:7b", "deepseek-r1:8b" |
"llamacpp" |
llama.cpp Server | Direct HuggingFace or local .gguf files with full GPU offload |
"Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:qwen2.5-coder-7b-instruct-q4_k_m.gguf" or "/content/model.gguf" |
"vllm" |
vLLM | High-speed Safetensors / AWQ / GPTQ models & parallel throughput | "Qwen/Qwen2.5-Coder-7B-Instruct-AWQ", "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B" |
"sglang" |
SGLang | Fast RadixAttention prompt caching & multi-turn agent loops | "Qwen/Qwen2.5-Coder-7B-Instruct" |
"transformers" |
HF Transformers | Instant startup using Colab's pre-installed PyTorch + optional 4-bit BnB | "Qwen/Qwen2.5-Coder-3B-Instruct" |
"auto" |
Auto-Detect | Automatically selects llamacpp for .gguf, vllm for org/repo, or ollama |
Any of the above |
| Colab Plan | GPU & VRAM | Realistic Model Size (Q4) | Realistic Context Window | Notes & ToS |
|---|---|---|---|---|
| Colab Free (Testing Only) | Tesla T4 (15 GB) | 3B – 8B (Spark-X2.6-4B, Qwen 2.5 7B) |
32k – 128k tokens | Frequent disconnects. Do not use for persistent tunnels; 14B+ or >128k will OOM. |
| Colab Pro / Pay-As-You-Go (Recommended) | Nvidia L4 (24 GB) or A100 (40 GB) | 7B – 27B/32B (Qwen 3.8 27B, Qwen 2.5 14B/32B, DeepSeek-R1 14B) |
64k – 128k (on 27B/32B) 128k – 256k (on 7B–14B) |
Paid compute units required for sustained interactive IDE sessions. |
| Colab Pro+ (Heavy Workloads) | Nvidia A100 (80 GB) | 32B – 70B (at 64k–128k) 7B – 14B 1M-Models (at 1M) |
Up to 1M (1,048,576) (Only on 1M-capable models like Qwen2.5-1M) |
High VRAM headroom + priority background execution. |
- Open Google Colab (using a Paid Colab Pro / Pro+ account for sustained usage) and create a new notebook.
- Under Runtime -> Change runtime type, select a GPU (T4, L4, or A100).
- Copy the code from
COLAB_SETUP.pyinto a code cell. - Set your desired backend, model, and context window at the top of the cell (matching your GPU's VRAM and the model's native context limit):
BACKEND = "ollama" # "ollama", "llamacpp", "vllm", "sglang", "transformers", or "auto" MODEL_ID = "SparkLLM/Spark-X2.6-4B:q4_k_m" # Model tag, HF repo, or .gguf path CONTEXT_WINDOW = 131072 # 32768 (32k), 65536 (64k), or 131072 (128k)
- Run the cell. Once initialized and pre-warmed in GPU VRAM, it will print your session credentials:
================================================================= OPENCOLAB BACKEND READY (MULTI-BACKEND) ================================================================= Inference Backend : ollama Active Model : SparkLLM/Spark-X2.6-4B:q4_k_m Context Window : 131072 tokens Tunnel Public URL : https://<random-subdomain>.trycloudflare.com Base URL : https://<random-subdomain>.trycloudflare.com/v1 API Bearer Key : <your-secure-random-key> =================================================================
Open PowerShell and run Apply-Colab-Tunnel.ps1 with your printed URL, Key, and Backend:
powershell -ExecutionPolicy Bypass -File ".\Apply-Colab-Tunnel.ps1" `
-TunnelUrl "https://<random-subdomain>.trycloudflare.com" `
-ApiKey "<your-secure-random-key>" `
-ModelId "SparkLLM/Spark-X2.6-4B:q4_k_m" `
-ModelName "Spark-X2.6 4B" `
-Backend "ollama" `
-ContextWindow 131072This single command:
- Automatically configures OpenCode's global (
~/.config/opencode/opencode.jsonc) and project settings. - Sets your custom context and output limits with tool calling enabled.
- Verifies live tunnel connectivity and reloads the OpenCode service instantly.
In your terminal:
opencodeSelect model colab/model and start coding!
Check out BRING_YOUR_OWN_MODEL_GUIDE.md for full instructions on:
- Ollama Registry Models (
BACKEND="ollama"):SparkLLM/Spark-X2.6-4B:q4_k_m,qwen2.5-coder,deepseek-r1,llama3.1, etc. - Native GGUF Server (
BACKEND="llamacpp"): Direct Hugging Faceowner/repo:file.gguf, HTTPS URLs, or Google Drive.gguffiles. - vLLM & SGLang High-Throughput Engines (
BACKEND="vllm"/BACKEND="sglang"): Full Safetensors, AWQ, and GPTQ models from Hugging Face. - Native Hugging Face Transformers (
BACKEND="transformers"): Zero-install PyTorch +TextIteratorStreamerwith optional 4-bitbitsandbytesquantization (LOAD_IN_4BIT=1).
- Cloudflare Quick Tunnels generate temporary HTTPS URLs.
- The FastAPI proxy rejects any request that lacks the generated
Authorization: Bearer <KEY>header. - The internal backend port (
11434) is bound strictly to127.0.0.1and never exposed directly to the public Internet. - No personal API keys or tunnel URLs are ever stored in version control.
This project is licensed under the PolyForm Noncommercial License 1.0.0 (Source-Available). See LICENSE and the Source-Available Guide for details.
Free for personal use, research, and non-commercial development.
| Symptom | Cause | Fix |
|---|---|---|
Tool calls show up as <tool_call>... text, or the agent loop stalls (llamacpp / transformers) |
The model emits tool calls as plain text (GLM / Hermes style) and llama_cpp.server does not parse them |
The proxy now converts them to OpenAI tool_calls automatically. Control it with TOOL_CALL_PARSER = "auto" | "on" | "off" (default auto: on for llamacpp and transformers) |
libcudart.so.12: cannot open shared object file (llamacpp) |
The CUDA 12 llama-cpp-python wheel on a CUDA 13 Colab image | Fixed: the CUDA 12 runtime libs bundled with pip's nvidia-* packages are added to LD_LIBRARY_PATH for the server |
Model architectures [...] are not supported (vllm / sglang) |
The engine has no implementation of the model architecture (e.g. Spark2_5ForCausalLM on vLLM 0.33) |
Use BACKEND = "llamacpp" with a GGUF or BACKEND = "transformers" instead |
| Setup cell hangs for minutes then fails | The backend crashed at startup | The cell now fails immediately and prints the last lines of /content/backend.log |