Koog 1.0 idioms, gotchas, and scaffolding skills for Kotlin agents on the JVM
71
88%
Does it follow best practices?
Impact
—
No eval scenarios have been run
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.
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