Engineering scalable systems, cloud-native applications, and AI-powered solutions.

How to Run Open-Weight LLMs Locally and Host Them on AWS EC2

Learn how to run open-weight LLM-based models locally using Ollama and LM Studio, and deploy them on AWS EC2 using inference servers like vLLM. Understand how model weights work, how OpenAI-compatible APIs simplify integration, and how to expose custom APIs for serving multiple users and specialized machine learning models.

Recorded Dec 5, 2026

Hugging Face hosts a wide range of open-weight models, including Large Language Models (LLMs), image and video generation models, and other general-purpose machine learning models.

In this post, let's focus specifically on LLMs and understand how you can run them on your local machine or host them on a cloud server to serve multiple users.

You can download these models from Hugging Face and run them locally using tools like Ollama or LM Studio, or deploy them on a cloud server (such as an AWS EC2 instance) to serve requests.

Do Hugging Face models expose an API by themselves, or do you need an inference server?

No. A Hugging Face model repository contains files such as model weights (e.g., .safetensors or .gguf), tokenizers, and configuration files. These files do not run a web server or expose HTTP endpoints on their own.

To expose an API, you need to run an inference server or runtime that loads the model files into memory and listens for incoming requests.

  • Local / quick setup: Ollama, LM Studio, or llama.cpp.
  • Production / multi-user servers (such as EC2): vLLM or SGLang are popular choices because they efficiently batch requests and provide an OpenAI-compatible API out of the box.

How do you communicate with an inference server?

Many modern LLM inference servers, such as vLLM and Ollama, provide OpenAI-compatible REST APIs. For example, an endpoint like POST /v1/chat/completions allows applications to send prompts using a familiar request format.

However, the available endpoints and supported features depend on the inference server and the model being served.

  • Check the serving runtime's documentation (e.g., Ollama, vLLM, or SGLang) for its exact API routes and request/response schemas.
  • Check the Hugging Face model card for prompt templates, supported tasks (chat, embeddings, vision, tool calling), and recommended parameters (such as temperature and context length).

How can you connect your application to an LLM hosted on an AWS EC2 instance?

You have two common approaches when hosting an inference server on an EC2 instance.

Option A: Connect your application directly to the inference server (e.g., vLLM or Ollama)

If your inference engine already exposes an endpoint such as /v1/chat/completions, your backend or client application can communicate with it using standard HTTP requests or an OpenAI-compatible SDK configured with your server's base_url.

Option B: Put a thin backend API (such as FastAPI) in front of the inference engine

In production, developers often put their own web API (FastAPI, Express, etc.) in front of the model server to handle additional responsibilities, such as:

  • Authentication and rate limiting: API keys, user quotas, billing.
  • Input validation and guardrails: Prompt size limits, filtering.
  • Business logic: Fetching user context, RAG retrieval, logging.
  • Routing requests to the model server: Forwarding prompts to http://localhost:8000/v1/chat/completions and streaming or returning the response.

Do all Hugging Face models support OpenAI-compatible APIs?

No. You don't necessarily need an OpenAI-compatible API. Older and non-chat models available on Hugging Face are typically loaded using the Python transformers library and served through your own web API (such as FastAPI).

Why isn't an OpenAI-compatible API universal?

  • What does OpenAI-compatible actually mean? An OpenAI-compatible API is an API format supported by modern inference servers, with familiar routes such as /v1/chat/completions and request structures such as messages. Tools like vLLM and Ollama support this format so applications can switch between different LLM providers more easily. The models themselves don't know anything about OpenAI.
  • Older and specialized models: Models developed for tasks other than conversational AI, such as BERT (classification and named entity recognition), T5/BART (translation and summarization), Whisper (audio transcription), or Stable Diffusion (image generation), do not necessarily accept a list of chat messages. Instead, they expect task-specific inputs, such as text, audio, or images, and return corresponding outputs, such as labels, transcriptions, embeddings, or generated images.

How do you host non-chat models on EC2?

Instead of using an LLM-specific inference server like Ollama or vLLM, you can load the model directly into Python using Hugging Face's transformers library and wrap it in a standard web framework like FastAPI.

Your FastAPI application can expose endpoints based on the model's task, accept the required inputs, run inference, and return the results to clients.

Final Thoughts

With open-weight models becoming increasingly accessible, running and hosting LLMs is no longer limited to large AI companies. Tools like Ollama, vLLM, and Hugging Face Transformers make it easier to experiment with models locally or deploy them on cloud infrastructure like AWS EC2 to serve your own applications.

The key is understanding how models are loaded, how inference servers work, and how APIs connect everything together.