This article contains affiliate links. If you buy through them, we may earn a commission at no extra cost to you.
Fine-Tune a Small AI Model on RunPod and Serve It via n8n — in 30 Minutes
You don't need a data-center contract or a machine learning PhD to run your own fine-tuned language model. With Runpod — a pay-per-use GPU cloud built for AI workloads — you can spin up a powerful training environment in minutes, fine-tune a compact open-source model on your own data, and expose the result as a clean REST API. Then you wire that API into n8n, the open-source workflow automation tool with 400+ integrations, and suddenly your custom model is powering real automations: Slack bots, email classifiers, internal Q&A tools, and more.
This guide walks you through the entire pipeline from zero to live API in roughly 30 minutes of active work.
What You Will Build
- A fine-tuned version of a small open-source LLM (we use TinyLlama-1.1B as the example)
- A vLLM or Ollama inference server running on a RunPod pod
- A public HTTPS endpoint for that model
- An n8n workflow that sends prompts to the endpoint and processes the response
Prerequisites
- A RunPod account (free to create; you pay only for GPU seconds used)
- A training dataset in JSONL format — even 200–500 instruction-response pairs are enough for a small model
- An n8n instance — self-hosted or n8n Cloud
- Basic comfort with a terminal and Python
Step 1 — Launch a RunPod GPU Pod for Fine-Tuning
Log in to RunPod and click + New Pod. For fine-tuning TinyLlama you need at least an RTX 3090 (24 GB VRAM); a single A100 40 GB is more comfortable and usually costs under $2/hour.
- Select the RunPod PyTorch 2.1 template — it comes with CUDA, Python 3.10, and pip pre-installed.
- Set disk to at least 30 GB (model weights + dataset + adapter checkpoints).
- Enable Expose HTTP Ports and add port 8000 (inference later) and 8888 (optional Jupyter).
- Click Deploy and wait about 90 seconds for the pod to start.
Connect via the RunPod web terminal or SSH. You are now on a GPU machine with no ongoing commitment — you stop the pod and billing stops.
Step 2 — Prepare the Training Environment
Inside the pod terminal, install the fine-tuning stack:
pip install -q transformers datasets peft trl accelerate bitsandbytes
Upload your JSONL dataset using the RunPod file manager or scp. Each line should follow the instruction-tuning format:
{"instruction": "Classify the sentiment", "input": "I love this product!", "output": "positive"}
Step 3 — Fine-Tune with QLoRA (the Fast Way)
QLoRA (Quantized Low-Rank Adaptation) lets you fine-tune a 1–7 B model on a single consumer GPU in minutes rather than hours by training only a small set of adapter weights on top of a 4-bit quantized base model.
Create a file finetune.py and paste the following minimal script:
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig
from trl import SFTTrainer
from datasets import load_dataset
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
dataset = load_dataset("json", data_files="data.jsonl", split="train")
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, device_map="auto")
lora_config = LoraConfig(r=8, lora_alpha=16, target_modules=["q_proj","v_proj"], lora_dropout=0.05)
trainer = SFTTrainer(
model=model,
train_dataset=dataset,
dataset_text_field="output",
peft_config=lora_config,
args=TrainingArguments(output_dir="adapter", num_train_epochs=3,
per_device_train_batch_size=4, fp16=True,
logging_steps=10, save_strategy="epoch")
)
trainer.train()
trainer.save_model("adapter")
Run it with python finetune.py. On an RTX 3090 with 300 examples this finishes in roughly 8–12 minutes.
Step 4 — Merge and Serve the Model with Ollama
After training, merge the LoRA adapter into the base model and serve it:
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Merge weights (using a helper script from PEFT)
python -c "
from peft import AutoPeftModelForCausalLM
import torch
model = AutoPeftModelForCausalLM.from_pretrained('adapter', torch_dtype=torch.float16)
merged = model.merge_and_unload()
merged.save_pretrained('merged_model')
"
# Create a Modelfile and push to Ollama
echo 'FROM ./merged_model' > Modelfile
ollama create my-custom-model -f Modelfile
ollama serve &
Ollama now listens on port 11434. Because you exposed port 8000 earlier, forward with:
ollama serve --host 0.0.0.0 --port 8000 &
RunPod gives you a public HTTPS URL in the format https://<pod-id>-8000.proxy.runpod.net. Test it:
curl https://<pod-id>-8000.proxy.runpod.net/api/generate \
-d '{"model":"my-custom-model","prompt":"Classify: Great product!","stream":false}'
If you see a JSON response with a response field, your model is live as an API.
Step 5 — Connect the API to n8n
Now the fun part: wire the model into an automated workflow using n8n, which lets you self-host for free or use n8n Cloud with 400+ ready-made integrations.
- Open your n8n canvas and add an HTTP Request node.
- Set Method to
POSTand URL to your RunPod proxy URL (/api/generate). - Under Body, choose JSON and paste:
{ "model": "my-custom-model", "prompt": "{{ $json.text }}", "stream": false } - Add a Set node after it to extract
{{ $json.response }}into a clean field. - Connect any trigger you like — a Webhook node, a Gmail trigger, a Slack message, or a scheduled Cron.
Example use cases you can build in under 5 minutes once the connection is live:
- Email triage: Incoming email → your model classifies intent → route to the right team in Slack
- Support ticket tagging: New Zendesk ticket → model assigns category → update ticket field
- Internal FAQ bot: Webhook from a web form → model answers → reply via email or Teams
Cost Estimate
- Fine-tuning run (~12 min on RTX 3090): ≈ $0.08–0.15
- Inference pod (RTX 3090, running 8 h/day): ≈ $1.60–2.00/day
- Tip: Stop the pod when not in use. Use a RunPod Serverless endpoint for production — you only pay per request.
Tips for Production Readiness
- Switch from Ollama to vLLM for higher throughput on shared endpoints.
- Store your merged model weights in RunPod's Network Volume so you don't re-upload on every pod restart.
- Add an API key header in Ollama or place an n8n Webhook with a secret token in front of the endpoint.
- Use n8n's built-in error handling nodes to retry failed inference calls automatically.
Summary
In about 30 minutes of active work you have: a domain-specific fine-tuned LLM, a live HTTPS inference endpoint on GPU hardware, and an n8n workflow that any team member can trigger without touching a command line. The whole stack costs cents to train and a couple of dollars per day to run — less than a SaaS AI API for many workloads.
If you want to run, fine-tune and serve your own open-source AI models without a long-term commitment, you can get started via Runpod — including pay-per-use GPU cloud access — to build and deploy custom AI endpoints at a fraction of typical cloud costs.