Kest

Search

Search the documentation

AI

Agents

A model that uses tools until it has an answer.

An agent is the loop that connects a model to your tools. It asks the model; if the model asks for a tool, the agent runs it, shows the model the result and asks again; when the model answers without asking for a tool, that is the final answer.

val agent = ai.agent(system = "You help customers with their orders. Be brief.", maxSteps = 6)
val result = agent.run("Where is order 1042?")
 
result.text            // the final answer
result.steps           // each step: what the model said, which tools it called, what they returned
result.usage           // tokens used in total
result.stoppedBy       // why it stopped: the model answered, or the step limit
Loading diagram…

A conversation

Continue where you left off by passing the earlier messages:

val first = agent.run("Where is order 1042?")
val second = agent.run("And order 1043?", history = first.messages)      // the agent remembers the conversation you pass in

Kest keeps no memory by itself: you store result.messages (in a session or a database) and pass them back. Trim old messages to keep prompts short and cheap.

Limits are essential

A model can loop, or ask for hundreds of calls. Limits keep cost and risk bounded:

LimitDefaultStops
maxSteps10a model that keeps calling tools forever
maxToolCallsPerStep8one that asks for hundreds of calls at once
maxTokensprovider defaultvery long answers

stoppedBy tells you which limit was reached; treat it as "no good answer" and respond accordingly.

Where to run it

The loop is synchronous and runs in the caller's thread, and each step is a network call: a request may take many seconds. For anything long, run the agent from a queue job or a workflow and notify the user when done. Put a time limit and circuit breaker around the model call.

Look inside

result.steps lists every tool call and its result, which is exactly what you need to audit what the agent did and to debug a wrong answer. Log it (without private data) and consider storing it for support.

There is no built-in memory beyond the history you pass, and no streaming.

Read more