Skip to main content

ModelProtocol — The Thinking Engine

The model is the entity’s thinking engine. It generates text, embeds vectors, and drives behavior. Swap the model and the entity thinks differently — that’s a feature, not a limitation.

What Models Do

A model provider wraps an external API or local inference engine behind a standard interface. The rest of the system never talks to Anthropic or Ollama directly — it talks to ModelProtocol, and the model handles the translation. The Entity creates an InferenceService wrapper around the model and passes it down to stack components. Components never see ModelProtocol directly — they get the narrow InferenceService interface.

ModelProtocol Interface

Supporting Types

InferenceService

Stack components don’t need the full ModelProtocol. They need two things: generate text and embed vectors. InferenceService is that narrow interface.

How InferenceService Works

When you call entity.set_model(model), the Entity:
  1. Stores the model reference
  2. Creates an _InferenceServiceImpl wrapping it
  3. Calls stack.on_model_changed(inference) on the active stack
  4. The stack propagates to all components via component.set_inference(inference)
The _InferenceServiceImpl routes infer() to model.generate() and uses a local HashEmbedder for embed() by default.

HashEmbedder Fallback

When no model-level embedding is available, the system uses a local hash-based n-gram embedder. This provides functional (if lower-quality) semantic search without any external API:
  • Provider ID: ngram-v1
  • No network calls required
  • Deterministic (same input always produces same vector)
  • Lower quality than model-based embeddings, but always available

Built-in Implementations

AnthropicModel

Wraps the Anthropic Python SDK for Claude models.
API Key Resolution (in priority order):
  1. Explicit api_key= parameter
  2. CLAUDE_API_KEY environment variable
  3. ANTHROPIC_API_KEY environment variable
CLI Usage:
Capabilities:
  • 200k context window
  • Tool use, vision, streaming
  • System messages extracted to top-level system parameter (Anthropic API convention)
Requirements: pip install anthropic or pip install kernle[anthropic]

OllamaModel

Connects to a local Ollama instance via HTTP REST API.
Capabilities:
  • Configurable context window (default 8192)
  • Streaming support
  • No tool use or vision (model-dependent)
Requirements: pip install requests and a running Ollama server

Entry Point Registration

Model implementations register in the kernle.models entry point group:
The core discovers installed models via discover_models() and can instantiate them on demand.

Building a Custom Model Provider

To integrate a new model provider, implement the ModelProtocol interface:
Register it as an entry point:
Then bind it to an entity:

Auto-Configuration

For CLI usage, Kernle can auto-configure a model from environment variables. The process run and process exhaust commands do this automatically. Detection priority:
  1. KERNLE_MODEL_PROVIDER env var (forces a specific provider)
  2. CLAUDE_API_KEY or ANTHROPIC_API_KEY → Anthropic
  3. OPENAI_API_KEY → OpenAI
  4. No key → graceful degradation (no model)
Override the model name with KERNLE_MODEL: