Skip to main content

Ollama

LogicGrid talks to a local Ollama server over its REST API. By default the client connects to http://localhost:11434.

Pull a model once:

ollama pull llama3.2

Use it from LogicGrid

There are two equivalent ways to instantiate the Ollama LLM client.

using LogicGrid.Core.Llm;

var llm = LlmClientBase.Ollama("llama3.2");

Custom base URL (Ollama on another host or in Docker):

var llm = LlmClientBase.Ollama(
model: "llama3.2",
baseUrl: "http://192.168.1.50:11434");
ParameterTypeDefaultNotes
modelstring"llama3.2"Ollama model tag. Pull it first with ollama pull <model>.
baseUrlstring"http://localhost:11434"Ollama HTTP endpoint. Override for Docker, remote hosts, or reverse proxies.

Option 2 — direct construction

using LogicGrid.Core.Providers;

var llm = new OllamaClient(
baseUrl: "http://localhost:11434",
defaultModel: "llama3.2");
ParameterTypeDefaultNotes
baseUrlstring"http://localhost:11434"Same as the factory's baseUrl.
defaultModelstring"llama3.2"The model used when the agent or call site doesn't override it.

The factory and the constructor produce equivalent clients — the factory is just a more discoverable entry point. Use direct construction when you need a custom HttpClient (next section) or are wiring up DI by hand.

Custom HttpClient (proxies, timeouts, headers)

OllamaClient accepts a pre-configured HttpClient. Use this when you need a corporate proxy, a longer timeout, or a custom header (for example, when fronting Ollama with a reverse proxy that requires authentication).

using System.Net;
using System.Net.Http;
using LogicGrid.Core.Providers;

var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://corporate-proxy:8080"),
UseProxy = true,
};

var http = new HttpClient(handler)
{
Timeout = TimeSpan.FromMinutes(5),
};
http.DefaultRequestHeaders.Add("X-Forwarded-User", "service-account");

var llm = new OllamaClient(
httpClient: http,
baseUrl: "https://ollama.internal.example",
defaultModel: "llama3.2");

Tool calling

LogicGrid ships two tool-calling strategies:

  • PromptSchemaStrategy (default) — describes tools in the system prompt and parses JSON tool calls out of the LLM's text response. Works with every model.
  • NativeToolCallingStrategy — uses the provider's native tool-call API. Stronger when the model supports it.

Some Ollama models support native tool calling (qwen2.5, llama3.1, llama3.2 and others); many do not. Stay on the prompt-schema default unless you've verified the specific model.

Full example — calculator tool over Ollama

using LogicGrid.Core.Agents;
using LogicGrid.Core.Llm;
using LogicGrid.Core.Tools;
using LogicGrid.Tools.Tools;

var llm = LlmClientBase.Ollama("llama3.2");

IAgent math = new Agent<string>(
name: "Mathlete",
description: "Solves arithmetic.",
systemPrompt: "Use the calculator tool when the user asks for a number.",
llm: llm,
tools: new ToolBase[] { new CalculatorTool() });

Console.WriteLine(
await math.RunAsync(
input: "What is (17 * 23) + 91?",
ctx: new AgentContext().WithLogging()));
09:14:02.118 [INF] [Mathlete] started | input: What is (17 * 23) + 91?
09:14:02.420 [INF] [Mathlete] tool call → calculator { "expression": "(17 * 23) + 91" }
09:14:02.430 [INF] [Mathlete] tool result | 482
09:14:03.180 [INF] [Mathlete] completed | output: (17 * 23) + 91 = 482.

(17 * 23) + 91 = 482.

For the full strategy reference and how to switch to native, see Tool calling strategy.

Embeddings

Same two-way pattern as the LLM client.

using LogicGrid.Memory.Embeddings;

var embedder = EmbeddingClientBase.Ollama("nomic-embed-text");
var vector = await embedder.EmbedAsync("Hello, world.");
ParameterTypeDefaultNotes
modelstring"nomic-embed-text"Embedding model tag. Recommended: nomic-embed-text (768 dims), mxbai-embed-large (1024 dims), bge-m3 (multilingual).
baseUrlstring"http://localhost:11434"Ollama HTTP endpoint.
dimensionsint0Vector size. 0 = discover on first call (one extra round-trip). Pass the known dimension to skip discovery.

Option 2 — direct construction

Mirrors OllamaClient exactly: baseUrl first, then defaultModel.

using LogicGrid.Memory.Embeddings;

var embedder = new OllamaEmbeddingClient(
baseUrl: "http://localhost:11434",
defaultModel: "nomic-embed-text",
dimensions: 768);
ParameterTypeDefaultNotes
baseUrlstring"http://localhost:11434"Ollama HTTP endpoint.
defaultModelstring"nomic-embed-text"Embedding model used when the call site doesn't override it.
dimensionsint0Vector size. 0 = discover on first call.

A second overload accepts a custom HttpClient for proxies, longer timeouts, or DI:

using System.Net.Http;
using LogicGrid.Memory.Embeddings;

var http = new HttpClient { Timeout = TimeSpan.FromMinutes(2) };

var embedder = new OllamaEmbeddingClient(
httpClient: http,
baseUrl: "http://localhost:11434",
defaultModel: "nomic-embed-text",
dimensions: 768);
ParameterTypeDefaultNotes
httpClientHttpClient(required)Pre-configured client.
baseUrlstring"http://localhost:11434"Same as above.
defaultModelstring"nomic-embed-text"Same as above.
dimensionsint0Same as above.

Troubleshooting

  • Connection refused — Ollama isn't running. Start it from the menu bar or ollama serve.
  • model not found — Run ollama pull <model> first.
  • Slow first request — Models are loaded lazily on first call. Run a warm-up CallAsync at startup if first-request latency matters.