chi
10/29/2025, 9:30 AMAnastasiia Zarechneva
10/29/2025, 9:41 AMchi
10/29/2025, 10:10 AMAnastasiia Zarechneva
10/29/2025, 10:11 AMchi
10/29/2025, 4:55 PM// create ai client
val openAiClient = OpenAILLMClient(apiKey = config.apiKey)
// wrap with retry client
val retryingClient = RetryingLLMClient(
delegate = openAIClient,
config = RetryConfig.PRODUCTION
)
// BaseWithFallbackPromptExecutor is just another PromptExecutor and calls the base executor in its overrides with logging for debug purpose as well as handles failures, errors, etc.
val baseExecutor = SingleLLMPromptExecutor(retryingClient)
val promptExecutor = BaseWithFallbackPromptExecutor(
delegate = baseExecutor,
tag = "KoogAgentLLM[OpenAI:${config.modelId}]"
)
// model
val lmModel = OpenAIModels.Chat.GPT4o
prompt = prompt(id = "android_accessibility_agent") {
system(DEFAULT_SYSTEM_PROMPT.trimIndent())
}
val agentConfig = AIAgentConfig(
prompt = prompt,
model = model,
maxAgentIterations = 20 //for debug but far from useful, my problem
)
val strategyBundle = buildStrategyBundle(mode) // single run / graph
val toolRegistry = factory.create() // returns a ToolRegistry
// To run an instructions
agentSession.scope.launch {
val aiAgent = AIAgent(
promptExecutor = promptExecutor,
agentConfig = agentConfig,
strategy = strategyBundle.strategy,
toolRegistry = toolRegistry,
installFeatures = strategyBundle.installFeatures
)
val result = runCatching {
agent.run(inputInstruction)
}
...
}
// functions used above
fun buildStrategyBundle(mode..): StrategyBundle {
val strategy = when (mode) {
PLAN_EXECUTE -> planExecuteStrategy()
SINGLE_RUN -> singleRunStrategy()
}
val install: GraphAIAgent.FeatureContext.() -> Unit = {
install(PlanExecutionTelemetryFeature) {
sink = telemetrySink
planNodeNames =
if (mode == PLAN_EXECUTE) setOf("plan_phase") else emptySet()
executeNodeNames =
if (mode == PLAN_EXECUTE) setOf("execute_llm","execute_tool", "send_tool_result" ) else emptySet()
}
}
return StrategyBundle(strategy = strategy, installFeatures = install)
}
private fun planExecuteStrategy(): AIAgentGraphStrategy<String, String> =
strategy("plan_execute") {
val planNode by nodeLLMRequest(name = "plan_phase", allowToolCalls = false)
val executeLlm by nodeLLMRequest(name = "execute_llm")
val executeTool by nodeExecuteTool(name = "execute_tool")
val sendToolResult by nodeLLMSendToolResult(name = "send_tool_result")
edge(nodeStart forwardTo planNode)
edge(planNode forwardTo executeLlm onAssistantMessage { true } )
edge(executeLlm forwardTo executeTool onToolCall { true } )
edge(executeLlm forwardTo nodeFinish onAssistantMessage { true } )
edge(executeTool forwardTo sendToolResult)
edge(sendToolResult forwardTo nodeFinish onAssistantMessage { true } )
edge(sendToolResult forwardTo executeTool onToolCall { true } )
}
kotlin
data class StrategyBundle(val strategy: AIAgentGraphStrategy<String, String>, val installFeatures: GraphAIAgent.FeatureContext.() -> Unit)
object PlanExecutionTelemetryFeature :
AIAgentGraphFeature<PlanExecutionTelemetryFeatureConfig, PlanExecutionTelemetryFeatureImpl> {...}
```chi
10/29/2025, 4:56 PMAndrey Bragin
10/30/2025, 11:54 AMEventHandler feature (or a handleEvents shorthand, it’s the same) to add listeners for certain events to log event details. E.g. check this example from our simple-examples . This should give you more observability into what the agent actually does.
I would assume that this is actually the root problem:
> I’m using gpt-4o-mini … I’ve given it a lot of tools and a detailed system prompt
gpt-4o-mini is a relatively small model, and my guess is that it just gets lost in all these tools and instructions and goes around in circles. Even with bigger models giving them a lot of tools can confuse the model. So my suggestions would be to try either narrowing the scope or using a smarter model. Another good approach would be to use subgraphWithTask to break down a complex task into a set of steps with clearer scope and smaller set of tools. You can check another simple example demonstrating how to use subgraphWithTask here, or, for a more advanced strategy, you can check out this trip planning agent examplechi
10/30/2025, 2:03 PM