Koog 1.0 idioms, gotchas, and scaffolding skills for Kotlin agents on the JVM
87
88%
Does it follow best practices?
Impact
87%
1.85xAverage score across 45 eval scenarios
Advisory
Suggest reviewing before use
Process steps in order. Do not skip ahead.
implementation("ai.koog:agents-features-tokenizer:1.0.0")
implementation("ai.koog:prompt-tokenizer:1.0.0") // provider tokenizersThe prompt-tokenizer module ships tokenizers for the major providers — they compute token counts before the LLM call, which is what the budgeting feature uses to gate requests.
Proceed immediately to Step 2.
import ai.koog.agents.features.tokenizer.Tokenizer
val agent = AIAgent(
promptExecutor = ...,
llmModel = OpenAIModels.Chat.GPT4o,
systemPrompt = "...",
) {
install(Tokenizer) {
// The tokenizer is selected per model by default; override if your provider needs custom counting
runBudget = 50_000 // hard cap on tokens consumed across the whole agent run
perNodeBudget = 8_000 // optional finer-grained cap per node
onBudgetExceeded = BudgetAction.Abort // or .CompressHistory, .DowngradeModel
}
}Budgets are inclusive — they count prompt tokens AND completion tokens. A 50k run budget against a 10k system prompt leaves 40k for the rest of the run.
Proceed immediately to Step 3.
BudgetAction.Abort — throws an exception, agent run ends with an error. Use when overrunning the budget is a bug, not an expected conditionBudgetAction.CompressHistory — runs a history compression strategy (see manage-state) to reclaim budget, then continues. Use for long-running agents where the budget is softBudgetAction.DowngradeModel — swaps to a cheaper model for subsequent calls. Use when output quality can degrade gracefullyFor finer-grained behavior, hook the tokenizer's events through handle-agent-events — the tokenizer emits onBudgetWarning events before exhaustion, so you can take custom action (notify, switch tools, log).
Proceed immediately to Step 4.
If OpenTelemetry is installed (add-observability), the tokenizer's token counts surface alongside the built-in gen_ai.client.token.usage metric — dashboards already targeted at that metric pick up budget data without changes.
If you only have the event handler installed (handle-agent-events), pair it with an onBudgetWarning callback to log warnings to stdout during development.
Finish here.
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10
scenario-11
scenario-12
scenario-13
scenario-14
scenario-15
scenario-16
scenario-17
scenario-18
scenario-19
scenario-20
scenario-21
scenario-22
scenario-23
scenario-24
scenario-25
scenario-26
scenario-27
scenario-28
scenario-29
scenario-30
scenario-31
scenario-32
scenario-33
scenario-34
scenario-35
scenario-36
scenario-37
scenario-38
scenario-39
scenario-40
scenario-41
scenario-42
scenario-43
scenario-44
scenario-45
skills
add-observability
add-persistence
add-rag
add-structured-output
add-token-budgeting
add-tool
cache-llm-calls
define-prompt
domain-model-subtask-pipeline
references
enable-prompt-caching
handle-agent-events
manage-state
migrate-from-0-x
model-planner-subtasks
persist-chat-history
query-sql-from-agent
scaffold-agent
snapshot-and-restore
test-koog-agents
trace-agent-internals
use-attachments
use-functional-agent
use-llm-node-variants
use-planner
wire-a2a
wire-acp-server
wire-ktor-server
wire-mcp-server
wire-spring-boot