Defaults hide the wiring; advanced mode labels every cable before anyone reaches for scissors.
1. Motivation
Most applications can stay withhb.LLM(preset="chat").
The features here matter when transport, reproducibility, SDK interop, image handling, or concurrency must be explicit.
The same resolution order still applies: preset, model, provider, then gateway.
2. Choose a Gateway
The defaultopenai gateway uses the OpenAI Python SDK against an OpenAI-compatible endpoint.
openai.
3. Register a Runtime Shape
Add a provider in configuration when an existing gateway already speaks its API. RegisterLLMGateway only for a genuinely different runtime argument shape.
heavenbase.llm.gateways so LLMEngine.resolve(...) can validate it.
LLMEngine resolves policy, LLMSpec carries the result, and LLMGateway materializes transport.
4. Control Response Caching
HeavenBase stores normalized chat, embedding, and image responses in a dedicatedllm-cache workspace.
Caching is enabled by default and uses a deterministic policy.
cache=False, cache=True, or a cache mapping.
Executable tool loops skip text cache automatically.
Deterministic text and image writes require stable generation arguments.
Stochastic requests without a fixed seed bypass cache reads and writes.
5. Reuse and Export Clients
LLMSpec.hash_key() identifies the complete resolved request defaults.
client_key() includes only gateway client construction fields, allowing compatible LLM instances to reuse in-memory SDK clients.
to_client(), to_aclient(), and to_args() support openai, portkey, and bifrost.
Native Anthropic uses to_anthropic_client() and to_anthropic_aclient().
6. Generate and Normalize Images
imagen-local when Ollama serves z-image-turbo.
Reference images accept the same normalized inputs as multimodal chat:
LLMImage normalizes paths, bytes, base64, URLs, provider items, and generated results.
heavenbase.llm.image_url_timeout.
7. Repair Tool Calls
LLMToolCallRepair can strip Markdown fences, balance JSON brackets, fill missing required schema fields, and compact OpenAI-style tool arguments.
tool_call_repair={...} to hb.LLM(...) or repair_tool_calls=True to one chat call.
Strict mode raises when arguments cannot be parsed; non-strict mode returns the original string.
8. Use a Custom Provider
Thecustom preset is for an unbundled OpenAI-compatible endpoint:
base_url and a concrete model are required.
Use a custom gateway instead when the payload family itself is different.
9. Run Asynchronously
achat; synchronous chat rejects async callables.
Summary
- Gateways own transport shape after model and provider resolution.
- Caching is visible, deterministic, and independently configurable by namespace.
- SDK exports support interop without duplicating resolution.
- Images and async calls use the same resolved client model.

