Add koog module: a tool-calling agent, a strategy graph with a Java node and the Spring Boot starter on Koog 1.3.0, tested against a scripted OpenAI-compatible endpoint

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6FHj1ptP5Fej2eFDo
This commit is contained in:
Claude
2026-11-11 11:28:34 +05:30
parent 58eaf4c2e1
commit a620d91d0a
28 changed files with 867 additions and 0 deletions
@@ -0,0 +1,70 @@
package com.ankurm.koog;
import ai.koog.agents.core.agent.AIAgent;
import ai.koog.agents.core.agent.MermaidDiagramGenerator;
import ai.koog.agents.core.agent.entity.AIAgentEdge;
import ai.koog.agents.core.agent.entity.AIAgentGraphStrategy;
import ai.koog.agents.core.agent.entity.AIAgentNode;
import ai.koog.agents.core.tools.ToolRegistry;
import ai.koog.prompt.executor.clients.openai.OpenAIModels;
import ai.koog.prompt.executor.model.PromptExecutor;
/** Builds the agents used by the tests and by the Spring Boot service. */
public final class OpsAgents {
public static final String SYSTEM_PROMPT =
"You are an on-call assistant. Use the tools to answer; never guess a status.";
private OpsAgents() {}
/** The smallest useful agent: an executor, a model, a system prompt and a tool registry. */
public static AIAgent<String, String> firstAgent(PromptExecutor executor) {
return AIAgent.builder()
.promptExecutor(executor)
.llmModel(OpenAIModels.Chat.GPT4o)
.systemPrompt(SYSTEM_PROMPT)
.toolRegistry(ToolRegistry.builder().tools(new ServiceTools()).build())
.build();
}
/** The same agent with one extra, ordinary Java node in front of the model: a redactor. */
public static AIAgentGraphStrategy<String, String> redactingStrategy() {
var graph = AIAgentGraphStrategy.builder("redacting_single_run")
.withInput(String.class)
.withOutput(String.class);
// A node is a function of (input, context). This one never talks to the model.
var redact = AIAgentNode.builder("redact")
.withInput(String.class)
.withOutput(String.class)
.withAction((input, ctx) -> input.replaceAll("sk-[A-Za-z0-9]{8,}", "[REDACTED]"))
.build();
var callLLM = AIAgentNode.llmRequest("callLLM");
var executeTool = AIAgentNode.executeTools("executeTool");
var sendToolResult = AIAgentNode.llmSendToolResults("sendToolResult");
graph.edge(graph.nodeStart, redact);
graph.edge(redact, callLLM);
graph.edge(AIAgentEdge.builder().from(callLLM).to(executeTool).onToolCalls().build());
graph.edge(AIAgentEdge.builder().from(callLLM).to(graph.nodeFinish).onTextMessage().build());
graph.edge(executeTool, sendToolResult);
graph.edge(AIAgentEdge.builder().from(sendToolResult).to(executeTool).onToolCalls().build());
graph.edge(AIAgentEdge.builder().from(sendToolResult).to(graph.nodeFinish).onTextMessage().build());
return graph.build();
}
public static AIAgent<String, String> redactingAgent(PromptExecutor executor) {
return AIAgent.builder()
.graphStrategy(redactingStrategy())
.promptExecutor(executor)
.llmModel(OpenAIModels.Chat.GPT4o)
.systemPrompt(SYSTEM_PROMPT)
.toolRegistry(ToolRegistry.builder().tools(new ServiceTools()).build())
.build();
}
public static String diagram() {
return MermaidDiagramGenerator.INSTANCE.generate(redactingStrategy());
}
}
@@ -0,0 +1,11 @@
package com.ankurm.koog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OpsApplication {
public static void main(String[] args) {
SpringApplication.run(OpsApplication.class, args);
}
}
@@ -0,0 +1,18 @@
package com.ankurm.koog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OpsController {
private final OpsService service;
public OpsController(OpsService service) { this.service = service; }
@GetMapping("/ask")
public String ask(@RequestParam("q") String question) {
return service.ask(question);
}
}
@@ -0,0 +1,25 @@
package com.ankurm.koog;
import ai.koog.agents.core.agent.AIAgent;
import ai.koog.prompt.executor.model.PromptExecutor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* The starter gives us a PromptExecutor bean per configured provider. It does not give us an agent:
* we build one per request around the shared executor, so no run shares state with another caller.
*/
@Service
public class OpsService {
private final PromptExecutor executor;
public OpsService(@Qualifier("openAIExecutor") PromptExecutor executor) {
this.executor = executor;
}
public String ask(String question) {
AIAgent<String, String> agent = OpsAgents.firstAgent(executor);
return agent.run(question);
}
}
@@ -0,0 +1,48 @@
package com.ankurm.koog;
import ai.koog.agents.core.tools.annotations.LLMDescription;
import ai.koog.agents.core.tools.annotations.Tool;
import ai.koog.agents.core.tools.reflect.ToolSet;
import java.util.Map;
/**
* Two read-only tools over a hard-coded service inventory. The model never sees this class:
* Koog reads the annotations and the method signatures and turns them into tool definitions.
*/
@LLMDescription("Read-only questions about the health of our services")
public class ServiceTools implements ToolSet {
private static final Map<String, String> STATUS = Map.of(
"checkout", "DEGRADED",
"search", "HEALTHY",
"billing", "HEALTHY");
private static final Map<String, Double> ERROR_RATE = Map.of(
"checkout", 4.2,
"search", 0.1,
"billing", 0.3);
@Tool
@LLMDescription("Returns the current status of one service: HEALTHY, DEGRADED or DOWN")
public String getServiceStatus(
@LLMDescription("Service name, for example checkout") String service) {
String status = STATUS.get(service);
if (status == null) {
throw new IllegalArgumentException("Unknown service: " + service);
}
return status;
}
@Tool(customName = "error_rate")
@LLMDescription("Returns the percentage of failed requests for one service over the last N minutes")
public double getErrorRate(
@LLMDescription("Service name, for example checkout") String service,
@LLMDescription("Window in minutes") int minutes) {
Double rate = ERROR_RATE.get(service);
if (rate == null) {
throw new IllegalArgumentException("Unknown service: " + service);
}
return rate;
}
}
@@ -0,0 +1,4 @@
# The starter reads ai.koog.<provider>.*; setting OPENAI_API_KEY in the environment also activates it.
ai.koog.openai.api-key=${OPENAI_API_KEY:}
# Point the OpenAI client somewhere else (a proxy, a gateway, or the scripted server in the tests).
# ai.koog.openai.base-url=https://api.openai.com