I want to write a Koog agent that would write code...
# koog-agentic-framework
a
I want to write a Koog agent that would write code in an unpopular language. For this I want it to be able to check whether the code that it's written is correct and re-do the task if it is incorrect. What is the idiomatic way to do so in Koog? I can imagine something like: make a tool that would parse and typecheck the written code and make a loop in my strategy graph that asks sends to LLM errors and asks it to rewrite the code.
m
If by "tool" you mean a custom strategy node, what you describe could work. Tools are for when you want the LLM to call something independently; strategy nodes are for where you want the work to be done as part of your overall process defined by your graph-based strategy. If you have something that could be used both ways, you could create a tool that your node then runs. Another option is external orchestration: • You call Koog to execute a prompt to generate the code • You take the results and run it through the checker • If needed, you call Koog with a fresh prompt, asking it to revise the code
❤️ 1
k
Hi, Artem. It looks like you need a node to store generated code in a file, then another tool to read the code and tool to execute tests against the files.
❤️ 1
v
The most ideomatic way would be: 1. Split the tools into 2-3 groups a. (optional) preparationTools b. codingTools c. verificationTools (one or many — but still as a group) 2. Define focused subtasks with the access to only the relevant tools each a.
val coding by subgraphWithTask<String, String>(tools = codingTools, model = …, …) { input -> "You have to write the code to complete the task:\n $input" }
b.
val verification by subgraphWithVerification(tools = verificationTools, model = …, …) { input -> "You have to verify that: 1. The code is compiling. 2. Tests pass. 3. The original problem is ACTUALLY solved as requested." }
3. And code the loop:
Copy code
val codingStrategy by strategy<String, String>("coding") {
    val coding by subgraphWithTask<String, String>(
        tools = codingTools, model = …, …
    ) { input -> 
        "You have to write the code to complete the task:\n $input" 
    }

    val verification by subgraphWithVerification<String, String>(
        tools = verificationTools, model = …, …
    ) { input -> 
        """You have to verify that: 
           1. The code is compiling. 
           2. Tests pass. 
           3. The original problem is ACTUALLY solved as requested
           If something is wrong -- provide the feedback what has to be fixed or re-implemented.
          """
    }

    edge(nodeStart forwardTo coding)
    edge(coding forwardTo verification)
    edge(verification forwardTo nodeFinish onCondition { it.isSuccessful } transformed { it.input })
    edge(verification forwardTo coding onCondition { !it.isSuccessful   } transformed { it.feedback })
}
So, we’re continously running the verification step (with relevant verification tools, including code check, compillation, etc.) and either finishing the work if everything is alright or passing the feedback back to the
coding
stage (and this loop continous)
❤️ 2
Of course, you have to play with the tools, prompts, and the data types (maybe use not Strings but some structured descriptors of the changes), and the models of choice — to make it working the optimal way
👍 1
PS: also consider compressing the history between the stages (use
nodeLLMCompressHistory()
— https://docs.koog.ai/history-compression/, ideally with
RetrieveFactsFromHistory
where you can state which specific facts or information are important for the next steps), otherwise
verification
step would see ALL actions (and intermediate attempts, tool calls, tool responses, file contents, etc.) of the
coding
stage, and vice versa. So consider describing what is relevant and persisting only that information between logical subtasks. Otherwise — you’ll be risking to consume the LLM context very fast when modifying large files. See this article as a reference about the history compression (specifically for coding agent): https://blog.jetbrains.com/ai/2026/01/building-ai-agents-in-kotlin-part-5-teaching-agents-to-forget/ And this one about domain modeling and `subgraphWithTask`: https://medium.com/@vadim.briliantov/beyond-prompts-use-domain-models-to-rule-ai-agents-instead-89765faa6e46
❤️ 3