Kest

Search

Search the documentation

AI

Models

Anthropic, OpenAI-compatible providers, and your own.

A model is anything that implements ChatModel { fun chat(request: ChatRequest): ChatResponse }. You pick one when you configure the module; the rest of your code does not change.

AnthropicModel(apiKey = System.getenv("ANTHROPIC_API_KEY"), model = "claude-sonnet-5-5")
OpenAiCompatibleModel(apiKey = System.getenv("OPENAI_API_KEY"), model = "gpt-4o")                         // OpenAI
OpenAiCompatibleModel(apiKey = null, model = "llama3", baseUrl = "http://localhost:11434/v1")           // Ollama, vLLM, LM Studio: anything with /chat/completions

OpenAiCompatibleModel speaks the widely copied /chat/completions format, so it also works with many other providers and with models you run yourself (no data leaves your network).

Talk to a model directly

val reply = model.chat(ChatRequest(
    messages = listOf(ChatMessage.user("Summarise this order in one line: $orderJson")),
    system = "Be brief. Answer in English.",      // standing instructions
    maxTokens = 200,                              // the longest answer you will pay for
    temperature = 0.2,                            // 0 = predictable, higher = more varied
)).message.content
PartMeaning
messagesthe conversation so far: ChatMessage.user(...), assistant(...), tool(...)
systeminstructions that frame every answer (role, tone, rules)
maxTokensa cap on the length of the answer (a token is roughly three quarters of a word)
temperaturerandomness; use low values for extraction and classification
toolsfunctions the model may request (set for you by agents)

A ChatResponse has the message (text, and toolCalls when the model wants a tool), a stopReason and usage (input and output tokens, for cost tracking: log it or export it as a metric).

Errors

A bad key, a rate limit (the provider says "slow down") or an unreachable host becomes an AiException with the HTTP status. Wrap model calls in resilience: a time limit, retries with jitter for 429 and 5xx, a circuit breaker, and a fallback ("our assistant is busy, try again").

Your own provider

Implement ChatModel: turn ChatRequest into the provider's request and its answer into ChatResponse. Everything else (agents, tools, tests) works unchanged.

Verified against fake provider servers in the test suite; not run against the live Anthropic or OpenAI APIs from the repository (that needs your keys), so make a first real call in a small test of your own.

Read more