What You’ll Build
A simple coding agent that can read, edit, and list files in a project directory. By the end of this guide, you’ll understand:- Agent initialization and configuration
- Tool registration
- Prompt executor setup
- Event handling
- Running agents
dependencies {
implementation("ai.koog:koog-agents:1.0.0")
implementation("ai.koog:prompt-executor-openai:1.0.0")
}
import ai.koog.prompt.executor.llms.all.simpleOpenAIExecutor
val executor = simpleOpenAIExecutor(System.getenv("OPENAI_API_KEY"))
import ai.koog.agents.core.tools.ToolRegistry
import ai.koog.agents.ext.tool.file.*
import ai.koog.rag.base.files.JVMFileSystemProvider
val toolRegistry = ToolRegistry {
tool(ListDirectoryTool(JVMFileSystemProvider.ReadOnly))
tool(ReadFileTool(JVMFileSystemProvider.ReadOnly))
tool(EditFileTool(JVMFileSystemProvider.ReadWrite))
}
import ai.koog.agents.core.agent.AIAgent
import ai.koog.agents.core.agent.singleRunStrategy
import ai.koog.prompt.executor.clients.openai.OpenAIModels
val agent = AIAgent(
promptExecutor = executor,
llmModel = OpenAIModels.Chat.GPT4oMini,
toolRegistry = toolRegistry,
systemPrompt = """
You are a highly skilled programmer tasked with updating
the provided codebase according to the given task.
Your goal is to deliver production-ready code changes that
integrate seamlessly with the existing codebase.
""".trimIndent(),
strategy = singleRunStrategy(),
maxIterations = 100
)
import ai.koog.agents.features.eventHandler.feature.handleEvents
val agent = AIAgent(
promptExecutor = executor,
llmModel = OpenAIModels.Chat.GPT4oMini,
toolRegistry = toolRegistry,
systemPrompt = "...",
strategy = singleRunStrategy(),
maxIterations = 100
) {
handleEvents {
onToolCallStarting { ctx ->
println("Tool '${ctx.toolName}' called with args: ${ctx.toolArgs}")
}
}
}
Complete Example
Here’s the full working code:Main.kt
What’s Next?
- Adding Custom Tools — Learn to create custom tools for your agents
- Graph Workflows — Build complex multi-step workflows
- Testing Agents — Write tests for your agents
Key Concepts
AIAgent
The main orchestrator that executes strategies, manages tools, and handles LLM communication.ToolRegistry
Centralized, type-safe tool management using a builder pattern. Tools are registered withtool() and registries can be merged with the + operator.
PromptExecutor
Handles communication with LLM providers (OpenAI, Anthropic, Google, etc.).Strategy
Defines the agent’s execution workflow.singleRunStrategy() runs once and finishes, while graph strategies allow complex flows.