Thanks Jetbrains for this framework! I’m having a...
# koog-agentic-framework
c
Thanks Jetbrains for this framework! I’m having an issue where every simple query I run into an issue that maxIteration reached even when I set it to 20. My agent is one that would control a device via accessibility service, I’ve given it a lot of tools and a detailed system prompt, but when I give it an instruction like “go to walgreens.com on chrome and add an item to cart”, it immediately exhausts my max iterations, while I think I can make improvements for my prompt, is there a way to make use of this so that it never stops until it completes or fails and not have this issue
a
Hi @chi! Could you please share which model profile, which strategy and parameters you use? It will help finding out the issue. Thanks!
c
I’m using gpt-4o-mini and is is using the full capability set (tools, JSON schema modes, vision, completions/responses) , for my strategy I’m using a graph strategy although I have a setting for flipping to single so I’ve been testing both and getting same result. I currently have 20 max iterations set
a
Thanks! Could you please share the strategy/agent setup code?
👍 1
c
Some parts here if this makes sense;
Copy code
// 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 } )
    }
Copy code
kotlin
data class StrategyBundle(val strategy: AIAgentGraphStrategy<String, String>, val installFeatures: GraphAIAgent.FeatureContext.() -> Unit)

object PlanExecutionTelemetryFeature :
    AIAgentGraphFeature<PlanExecutionTelemetryFeatureConfig, PlanExecutionTelemetryFeatureImpl> {...}
```
Still exploring the framework and would appreciate any feedback as well
a
Do you see the agent log? You can use
EventHandler
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 example
kodee loving 1
🙏 1
c
Thanks for your detailed response! Would explore these!