LLM API

Piko's LLM service abstracts over Anthropic, OpenAI, Gemini, Grok, Mistral, Ollama, Voyage, and Zolt.ai. It supports plain completions, streaming, tool calling, structured JSON, embeddings, retrieval-augmented generation, cost tracking, budgets, rate limits, and response caching. For task recipes see how to LLMs and embeddings. Source of truth: wdk/llm/facade.go.

Service

FunctionReturns
llm.NewService(defaultProviderName string, opts ...ServiceOption) ServiceConstructs a new service.
llm.GetDefaultService() (Service, error)Returns the bootstrap-configured service.

Builders

func NewCompletionBuilder(service Service) *CompletionBuilder
func NewCompletionBuilderFromDefault() (*CompletionBuilder, error)
func NewEmbeddingBuilder(service Service) *EmbeddingBuilder
func NewEmbeddingBuilderFromDefault() (*EmbeddingBuilder, error)

Common fluent methods on CompletionBuilder include .Model(...), .Messages(...), .Tools(...), .ResponseFormat(...), .Stream(...), .Temperature(...), .MaxTokens(...), .Do(ctx). The full surface also covers RAG (WithRAGQuery, WithRAGMinScore, WithRAGFilter, WithRAGEmbeddingProvider, WithRAGEmbeddingModel, WithRAGQueryRewriter, WithRAGHybridSearch), vector context (WithVectorContext), tool choice, top-p, stop sequences, seeds, and per-call cost or rate-limit overrides. See wdk/llm/facade.go for the authoritative list.

Message helpers

NewSystemMessage(content string) Message
NewUserMessage(content string) Message
NewAssistantMessage(content string) Message
NewToolResultMessage(toolCallID, content string) Message
NewUserMessageWithImages(content string, images ...ContentPart) Message
NewUserMessageWithImageURL(content string, url, detail string) Message
NewUserMessageWithImageData(content string, data []byte, mimeType, detail string) Message

Tools and structured output

NewFunctionTool(name, description string, params JSONSchema) ToolDefinition
NewStrictFunctionTool(name, description string, params JSONSchema) ToolDefinition
ToolChoiceAuto() ToolChoice
ToolChoiceNone() ToolChoice
ToolChoiceRequired() ToolChoice
ToolChoiceSpecific(name string) ToolChoice

JSONSchemaDefinition and JSONSchema describe response formats. Set ResponseFormat.Type = ResponseFormatJSONSchema with ResponseFormat.JSONSchema to force a structured output.

Cost and budget

FunctionPurpose
NewCostCalculator()Cost calculator with the default pricing table.
NewCostCalculatorWithPricing(table *PricingTable)Custom pricing.
NewBudgetManager(store, calculator)Enforces spending limits.
DefaultPricingTableThe shipped pricing table.

BudgetStorePort persists spending counters. Provide a cache.Cache or custom backing store.

Rate limiting

FunctionPurpose
NewRateLimiter(store, opts...)Per-model, per-user, or global rate limit.
RateLimiterStorePortDriven port for counter state.
WithRateLimiterClock(clk)Injects a test clock.

Response cache

NewCacheManager(store, defaultTTL) returns a manager that short-circuits completions whose inputs match a previously cached request.

Memory

FunctionPurpose
NewBufferMemory(store, opts...)Fixed-size conversation buffer.
NewWindowMemory(store, opts...)Token-window buffer.
NewSummaryMemory(store, service, config)LLM-summarised long history.

Retrieval-augmented generation

FunctionPurpose
NewVectorStore(factory)Cache-backed vector store.
Service.NewIngest(namespace) *IngestBuilderMethod on Service (not a top-level function). Returns a fluent builder that loads, splits, transforms, and vectorises documents into namespace.
NewRecursiveFSLoader(fsys, patterns...)Filesystem document loader.
NewRecursiveCharacterSplitter(chunkSize, overlap) (SplitterPort, error)Token-agnostic chunker. Returns an error when chunkSize or overlap are invalid.
NewMarkdownSplitter(chunkSize, overlap, opts...) (SplitterPort, error)Markdown-aware chunker. Returns an error when configuration is invalid.
StripFrontmatter(), ExtractFrontmatter(opts...)Frontmatter transforms.
PrependChunkContext()Attaches chunk position metadata.
LLMQueryRewriter(opts...)Multi-query expansion.

Attach RAG to a completion with CompletionBuilder.WithRAG(...) options: WithRAGQuery, WithRAGMinScore, WithRAGFilter, WithRAGEmbeddingProvider, WithRAGEmbeddingModel, WithRAGQueryRewriter, WithRAGHybridSearch.

Errors

ErrProviderNotFound, ErrNoDefaultProvider, ErrProviderAlreadyExists, ErrStreamingNotSupported, ErrToolsNotSupported, ErrStructuredOutputNotSupported, ErrEmptyMessages, ErrEmptyModel, ErrInvalidTemperature, ErrInvalidTopP, ErrInvalidMaxTokens, ErrBudgetExceeded, ErrRateLimited, ErrMaxCostExceeded, ErrUnknownModelPrice, ErrProviderOverloaded, ErrProviderTimeout, ErrConversationNotFound.

Providers

Sub-packageBackend
llm_provider_anthropicClaude models.
llm_provider_openaiOpenAI GPT and o-series.
llm_provider_geminiGoogle Gemini.
llm_provider_grokxAI Grok.
llm_provider_mistralMistral.
llm_provider_ollamaLocal Ollama.
llm_provider_voyageVoyage AI embeddings.
llm_provider_zoltaiZolt.ai.

Ollama

llm_provider_ollama.NewOllamaProvider(config) performs no network requests or process startup. The first request connects to the server, starting ollama serve if AutoStart permits it. Start(ctx) and EnsureModels(ctx) allow explicit initialisation before requests arrive.

Config contains the following lifecycle limits.

FieldDefaultPurpose
StartupTimeout30 secondsWait for a spawned server to answer health checks.
ProbeTimeout2 secondsEach health or reachability request.
StopGracePeriod5 secondsTime allowed for a managed server to exit after an interrupt, before forced termination.

Image fetching

A non-nil Config.ImageFetch enables fetching image URLs from messages. Destinations must have publicly routable addresses. The provider rejects loopback, private, link-local, and cloud metadata destinations. Inline image data does not require URL fetching.

ImageFetchConfig fieldDefaultPurpose
MaxImages8Maximum image URLs per request. The provider rejects excess URLs before downloading any image.
MaxBytes20 MiBMaximum bytes per image.
Timeout30 secondsTime limit for each image fetch.
MaxRedirects5Maximum redirects per fetch. A negative value rejects all redirects.

Bootstrap options

OptionPurpose
piko.WithLLMProvider(name, provider)Registers a completion provider.
piko.WithDefaultLLMProvider(name)Marks the default completion provider.
piko.WithEmbeddingProvider(name, provider)Registers an embedding provider.
piko.WithDefaultEmbeddingProvider(name)Marks the default embedding provider.
piko.WithLLMService(service)Registers a fully configured service.

See also