Deep Learning & LLMLLM API

LLM Calls

The klyn.llm package supports OpenAI-compatible chat-completions providers, typed conversation history and token accounting. Configure a provider, send one request, then keep a conversation with LLMChatSession.

API Layers

Type Role
LLMProvider Provider contract. Implement complete() to integrate another transport.
OpenAIProvider HTTP implementation for OpenAI-compatible chat-completions endpoints.
LLMRequest / LLMResponse Low-level request and response objects.
LLMChatSession Conversation history, tool loop, token usage, and optional context compaction.
ToolRegistry Typed registry generated from annotated interfaces and their implementations.

Configuring a Provider

Keep credentials outside source control. The provider URL must be the API version root; the implementation appends /chat/completions automatically.

import klyn.collections
import klyn.llm

def requiredEnvironment(name as String) as String:
    value as String = Application.environmentVariables.get(name, "")
    if value == "":
        throw Exception("Missing environment variable: " + name)
    return value

provider = OpenAIProvider(
    baseUrl=requiredEnvironment("KLYN_LLM_URL"),
    apiKey=requiredEnvironment("KLYN_LLM_API_KEY"),
    model=requiredEnvironment("KLYN_LLM_MODEL"),
    temperature=0.2
)
export KLYN_LLM_URL="https://provider.example/v1"
export KLYN_LLM_API_KEY="..."
export KLYN_LLM_MODEL="model-id"
Provider compatibility

OpenAIProvider describes the wire protocol, not a mandatory vendor. It can target compatible local servers and hosted gateways. Provider-specific tool-call metadata returned by compatible gateways is preserved when the conversation is replayed.

One Low-Level Request

Use the request objects directly when the caller wants full control over the message list and does not need a managed conversation.

messages as ArrayList<LLMThreadMessage> = [
    LLMThreadMessage.system("Answer in one short paragraph."),
    LLMThreadMessage.user("What is static typing?")
]

response = provider.complete(LLMRequest(messages))
if response.message is not null:
    print(response.message.content)

print("tokens: " + response.totalTokens)

Managed Conversation

LLMChatSession retains system, user, assistant, and tool messages. Reusing the same session therefore gives the model the preceding conversation without rebuilding history by hand.

session = LLMChatSession(provider) \
    .withSystem("You are a concise Klyn programming assistant.")

print(session.ask("What does f:[1, 2, 3] create?"))
print(session.ask("Is that collection mutable?"))

print("messages: " + session.thread().size)
print("last request tokens: " + session.lastTotalTokens)

Complete Sample

samples/llm/llm.kn demonstrates the low-level request API. It is listed on the Code Examples page.

Next Step

Continue with Agents and Tools.