Free · WaitlistJoin the waitlist for the free 2-day Vibecoding & AI Agents Intensive (autonomous Hermes + DeepSeek agent · led by Jemal Hamidun, Head of AI at Alpina Digital & MIPT)Join the waitlist →
ReleaseAPI / Product · ActionableMachine Learning Mastery→ original

How to Serve a Local Llama 3 Classifier via FastAPI Without Paid Clouds

Labeling reviews and support tickets with an LLM is convenient, but paid cloud usage and latency quickly become a problem. Scikit-LLM turns Llama 3 running in Ollama into a regular classifier with fit and predict methods, and FastAPI exposes it at /predict. The model is not fine-tuned: it is simply given a list of allowed labels and picks one, much like a multiple-choice test. For a prototype of an internal labeling service, that is enough, and no paid API is required.

AI-processed from Machine Learning Mastery; edited by Hamidun News
How to Serve a Local Llama 3 Classifier via FastAPI Without Paid Clouds
Source: Machine Learning Mastery. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceMachine Learning Mastery ↗
Editor-in-Chief
Key metric #1Three test reviews received labels in a few seconds on the local model
Key metric #2Labels such as Battery Issue are saved to the scikit_llm_model.joblib file
Key metric #3The blocking call is moved to a thread with asyncio.to_thread

Text classification is one of the most down-to-earth tasks for LLMs. You need to sort support requests by topic, separate complaints about batteries from complaints about a shattered screen, and tag positive reviews. The problem starts when the prototype has to be shipped as a service: cloud API bills grow, latency jumps around, and the project is tied to a single vendor. A walkthrough on Machine Learning Mastery shows the minimal way to avoid this: the combination of Scikit-LLM, Ollama, and FastAPI runs entirely on your own machine.

What the stack is and how it works

Scikit-LLM is a Python framework that hides an LLM behind the familiar scikit-learn interface. You create a classifier, call fit with examples and labels, then call predict with new texts. For anyone who has already written scikit-learn code, the learning curve is nearly zero.

Ollama runs the model locally. The example uses llama3, which you need to download in advance. The key point is that Scikit-LLM connects to an OpenAI-compatible API by default, and Ollama can speak that same protocol. So it is enough to specify the address http://localhost:11434/v1/, put any string in place of the key, and name the model custom_url::llama3. No cloud key is needed, and you pay for no tokens.

An important detail: fit does not train anything here. The model's weights do not change. The classifier stores the list of allowed categories and inserts it into the request, just as if you had written a question with answer options in a prompt. The example has only three pairs: "The battery drains too fast" → Battery Issue, "I love the new camera" → Positive Feedback, "Screen is completely cracked" → Hardware Damage.

How it becomes a service

The work is split across two files. In train.py, a ZeroShotGPTClassifier is created, fit is called, and the result is saved with joblib to scikit_llm_model.joblib. The entire "training" takes moments, because what gets saved is essentially a configuration with a set of labels.

In app.py, a FastAPI application starts up. The model is loaded into memory once at startup via lifespan, not on every request. There is a single endpoint, POST /predict, which accepts JSON with a list of texts and returns a list of predictions. You can send several texts in one call.

Two engineering decisions are worth adopting. First: because the call to the LLM blocks the thread, the clf.predict call is moved to a background thread via asyncio.to_thread. Otherwise, one slow request would freeze the entire asynchronous server. Second: the Scikit-LLM configuration (the address and the placeholder key) had to be set again in app.py, because these are global process settings rather than part of the saved file. Forgetting this is a typical source of errors during deployment.

The check is simple: you run python app.py, open http://127.0.0.1:8000/docs, click "Try it out", and send three phrases: about a phone that dies by noon, about glass shattered on the sidewalk, and about the best device of your life. A few seconds later the labels Battery Issue, Hardware Damage, and Positive Feedback come back.

Limitations to keep in mind

The author himself names the main cost of this approach: the closed world. The classifier must assign any text to exactly one label from the list, even if the text is about something entirely different. A letter about delivery will also be stuffed into "Battery Issue" or "Hardware Damage" unless you add a suitable category, such as "Other". You have to add the fallback class yourself.

Beyond that, there are questions the article does not cover. Three training examples are only for demonstration; for a real system, the label sets and phrasings will need to be validated on your own data. Speed depends on hardware: "a few seconds" for three texts is a laptop-mode figure, not a production load. The service listens only on 127.0.0.1 and has no authentication, so it is far from ready for public use. Finally, the word "deterministic" in the text should be understood as "predictable within a set of answers": an LLM may still occasionally answer differently on borderline phrases.

Takeaways

The practical conclusion for a developer: if the task is labeling texts into predefined categories, you do not necessarily have to pay a cloud provider right away or collect a dataset for fine-tuning. A local model plus a fit/predict interface gives a working prototype in an evening, and FastAPI turns it into a service that is easy to connect to the rest of the system. Because Scikit-LLM is compatible with scikit-learn objects, you can also add preprocessing steps to this classifier within a shared pipeline. For the first stage, which is to test the hypothesis and calculate what such labeling costs on your own traffic, this is enough.

Story

Key Questions Answered

What the stack is and how it works?

Scikit-LLM is a Python framework that hides an LLM behind the familiar scikit-learn interface. You create a classifier, call fit with examples and labels, then call predict with new texts. For anyone who has already written scikit-learn code, the learning curve is nearly zero. Ollama runs the model locally. The example uses llama3, which you need to download in advance. The key point is that Scikit-LLM connects to an OpenAI-compatible API by default, and Ollama can speak that same protocol.

How it becomes a service?

The work is split across two files. In train.py, a ZeroShotGPTClassifier is created, fit is called, and the result is saved with joblib to scikit_llm_model.joblib. The entire "training" takes moments, because what gets saved is essentially a configuration with a set of labels. In app.py, a FastAPI application starts up. The model is loaded into memory once at startup via lifespan, not on every request.

What are the key details on Limitations to keep in mind?

The author himself names the main cost of this approach: the closed world. The classifier must assign any text to exactly one label from the list, even if the text is about something entirely different. A letter about delivery will also be stuffed into "Battery Issue" or "Hardware Damage" unless you add a suitable category, such as "Other". You have to add the fallback class yourself. Beyond that, there are questions the article does not cover.

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…