DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on Edited on

How to Run AI Models Locally with Ollama (When You Run Out of API Credits)

Running AI agents can become expensive, especially when you're constantly making API calls to providers such as OpenAI, Anthropic, and Hugging Face.

But what if you could run an LLM directly on your computer without paying for every API request?

That's where Ollama comes in.

Ollama allows you to download and run AI models locally on your machine. You can experiment with LLMs, build AI applications, and even develop AI agents without relying entirely on paid API providers.

Here's how to get started.

Step 1: Install Ollama

Download and install Ollama for your operating system (Windows, macOS or Linux).

Official installation instructions:

https://ollama.com/download

Step 2: Download a model locally

Once installed, open your terminal and pull a model.

For example, let's use Qwen3 8B:

ollama pull qwen3:8b
Enter fullscreen mode Exit fullscreen mode

This downloads the model to your computer so you can run it locally.

To test it:

ollama run qwen3:8b
Enter fullscreen mode Exit fullscreen mode

You can now start chatting with your local model directly from the terminal.

Explore other available models:

https://ollama.com/library

Step 3: Start the Ollama server

Ollama exposes a local API that applications can communicate with.

Run:

ollama serve
Enter fullscreen mode Exit fullscreen mode

The default API address is:

http://localhost:11434
Enter fullscreen mode Exit fullscreen mode

A common issue:

If you encounter this error:

listen tcp 127.0.0.1:11434:
bind: address already in use
Enter fullscreen mode Exit fullscreen mode

It usually means Ollama is already running in the background.

On Linux, you can investigate the process using:

sudo lsof -i :11434
Enter fullscreen mode Exit fullscreen mode

If Ollama is already running, you don't need to start another server.

Step 4: Connect Ollama to smolagents using LiteLLMModel

This is where things get interesting.

If you're following the Hugging Face smolagents tutorial, you may have initially configured your application to use InferenceClientModel.

What is InferenceClientModel?

It is a Python class provided by the smolagents library. It allows your application to communicate with models through Hugging Face Inference Providers.

However, in this example, we want to use a model running locally through Ollama instead.

We achieve this by switching to LiteLLMModel.

What exactly do you replace?

You don't replace a file or install another version of smolagents.

You replace the model initialization code in your existing Python script.

For example, your original code might look something like this:

from smolagents import InferenceClientModel

model = InferenceClientModel(
    model_id="some-model-id",
    token="YOUR_HUGGINGFACE_TOKEN"
)
Enter fullscreen mode Exit fullscreen mode

Change it to:

from smolagents import LiteLLMModel

model = LiteLLMModel(
    model_id="ollama_chat/qwen3:8b",
    api_base="http://127.0.0.1:11434",
    num_ctx=8192,
)
Enter fullscreen mode Exit fullscreen mode

Notice the changes:

  • Import LiteLLMModel instead of InferenceClientModel.
  • Use the Ollama model identifier.
  • Point api_base to your local Ollama server.
  • Configure the context window using num_ctx.

First, install the LiteLLM integration:

pip install 'smolagents[litellm]'
Enter fullscreen mode Exit fullscreen mode

If your project already has an agent configured, you can keep the rest of the agent code unchanged, provided the selected model supports the capabilities your agent requires.

For example:

from smolagents import ToolCallingAgent, LiteLLMModel

model = LiteLLMModel(
    model_id="ollama_chat/qwen3:8b",
    api_base="http://127.0.0.1:11434",
    num_ctx=8192,
)

agent = ToolCallingAgent(
    tools=[],
    model=model
)
Enter fullscreen mode Exit fullscreen mode

That's it! Your smolagents application can now communicate with a locally running model.

Step 5: Which Models Should You Run Based on Your Computer Specifications?

Not everyone has a powerful workstation with an expensive GPU.

The good news is that there are smaller models that can run on ordinary laptops.

Here are some models worth exploring.

Computer specifications Recommended models Suitable for
8GB RAM Llama 3.2 1B, Gemma 3 1B Basic chat, simple tasks
16GB RAM Llama 3.2 3B, Gemma 3 4B, Qwen3 4B Coding experiments, assistants
32GB RAM Qwen3 8B, Gemma 3 12B, Phi-4 14B Reasoning, coding, AI agents
64GB RAM Gemma 3 27B, larger quantized models More demanding AI workloads
64GB+ with capable GPU Larger models, including 30B+ models Advanced experimentation

These are approximate recommendations, not strict hardware requirements. Actual performance depends on quantization, GPU VRAM, context length and other applications running on your computer.

Some models worth trying

1. Llama 3.2 - Meta

Good for lightweight applications and experimentation.

ollama pull llama3.2:1b
Enter fullscreen mode Exit fullscreen mode

Or:

ollama pull llama3.2:3b
Enter fullscreen mode Exit fullscreen mode

2. Gemma 3 - Google

A good option for general-purpose tasks. It also supports image understanding.

ollama pull gemma3:4b
Enter fullscreen mode Exit fullscreen mode

For more capable hardware:

ollama pull gemma3:12b
Enter fullscreen mode Exit fullscreen mode

3. Qwen3 - Alibaba

Worth exploring for coding, reasoning and general-purpose applications.

ollama pull qwen3:4b
Enter fullscreen mode Exit fullscreen mode

For more capable machines:

ollama pull qwen3:8b
Enter fullscreen mode Exit fullscreen mode

4. Phi-4 - Microsoft

A model worth experimenting with for reasoning, mathematics and coding tasks.

ollama pull phi4
Enter fullscreen mode Exit fullscreen mode

It is a 14B parameter model, so give it sufficient memory.

5. Gemma 3 27B - Google

For developers with more powerful hardware who want to experiment with larger models.

ollama pull gemma3:27b
Enter fullscreen mode Exit fullscreen mode

This model is considerably more demanding and benefits from substantial available memory.

Step 6: A Few Technical Things You Should Understand

Before running local models in your projects, keep these considerations in mind.

1. RAM vs GPU VRAM

The number of parameters isn't the only thing that determines whether a model will run.

Your available RAM and GPU VRAM matter. A model can run using system memory, but inference may be considerably slower than using a suitable GPU.

2. Quantization matters

Quantization reduces the memory required to load a model by representing its weights using fewer bits.

For example, a Q4 quantized model generally requires less memory than its full-precision counterpart.

Ollama provides different quantized variants for many models.

3. Context window matters

The context window determines how much input and conversation history a model can process.

Increasing num_ctx increases memory requirements. Don't automatically set it to a very large value if your computer has limited memory.

4. Local models aren't always as capable as hosted models

A smaller local model may struggle with complex reasoning, coding and multi-step agent tasks.

Always evaluate the model against your actual use case.

5. Tool calling and agent compatibility

Running a model locally doesn't automatically make it a good AI agent.

Some models are better at following instructions, generating structured outputs and calling tools than others.

Test these capabilities before using a model in a production agent.

Useful References

Here are some resources to help you get started:

My Recommendation and Conclusion

If you're just getting started with local LLMs, don't rush to download the largest model available.

Start with a smaller model such as Llama 3.2 3B or Qwen3 4B. If you have 16GB-32GB RAM, experiment with Qwen3 8B.

My recommendation is to learn how to work with both hosted and local models.

Hosted models are useful when you need stronger reasoning and reliability. Local models are useful for experimentation, privacy-sensitive workflows and reducing dependence on paid API calls.

Also, remember that local inference isn't completely free. You're still paying for electricity, hardware and maintenance.

The bigger lesson?

You don't always need an expensive API subscription to start building AI applications and agents.

Learn to work with different models, understand their limitations, and choose the right one for the job.

Your hardware might be more capable of running AI than you think!

Top comments (0)