Overview
Thekoog-ktor module provides:
- Ktor Plugin: Easy installation and configuration
- Multi-provider Support: OpenAI, Anthropic, Google, MistralAI, OpenRouter, DeepSeek, Ollama
- Agent Configuration: Tools, features, and prompt customization
- Route Extensions: Convenient functions for LLM and agent interaction
- MCP Integration: JVM-specific Model Context Protocol support
- YAML/CONF Configuration: File-based or programmatic setup
- Lightweight: Minimal overhead for high-performance services
- Flexible: File-based or code-based configuration
- Type Safe: Full Kotlin DSL with compile-time safety
- Coroutine Native: Built for Kotlin coroutines
- Production Ready: Battle-tested in real applications
Installation
Add the Ktor integration dependency:gradle
Quick Start
Basic Setup
Install the Koog plugin in your Ktor application:koog-ktor/Module.md:48
Configuration
YAML Configuration
Configure providers inapplication.yaml:
koog-ktor/Module.md:32
CONF Configuration
Or use HOCON format inapplication.conf:
koog-ktor/Module.md:158
Programmatic Configuration
Override or extend file-based configuration:koog-ktor/Module.md:133
Route Extensions
Use extension functions in your routes for easy agent interaction:Direct LLM Calls
Execute prompts directly:examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt:99
Content Moderation
Moderate content before processing:examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt:78
AI Agent Execution
Execute agents with strategies:examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt:116
Agent Configuration
Configure agent behavior, tools, and features:koog-ktor/Module.md:216
MCP Integration
Configure Model Context Protocol integration (JVM only):examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt:72 and koog-ktor/Module.md:246
Complete Example
Here’s a complete Ktor application with tools and features:examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt
Configuration Reference
LLM Configuration
| Property | Type | Default | Description |
|---|---|---|---|
apikey | String | - | API key for authentication |
baseUrl | String | Provider default | Base URL for API endpoint |
timeout.requestTimeoutMillis | Long | 30000 | Request timeout |
timeout.connectTimeoutMillis | Long | 10000 | Connection timeout |
timeout.socketTimeoutMillis | Long | 30000 | Socket timeout |
Agent Configuration
| Property | Type | Default | Description |
|---|---|---|---|
model | LLModel | - | Default LLM model |
maxAgentIterations | Int | 100 | Maximum agent iterations |
registerTools | ToolRegistry | Empty | Tool registry builder |
prompt | Prompt | Empty | Default system prompt |
Best Practices
-
Use Environment Variables: Keep credentials secure
-
Configure Fallback: Always have a fallback provider
-
Handle Errors: Use try-catch for agent calls
-
Moderate Content: Always validate user input
-
Use Coroutines: Leverage Kotlin coroutines for concurrency
Common Use Cases
- REST APIs: AI-powered API endpoints
- Webhooks: Process incoming webhooks with agents
- Microservices: Lightweight AI microservices
- Real-time Chat: WebSocket-based chat applications
- Batch Processing: Background job processing with agents
Platform Support
- JVM: Full support (including MCP)
- Native: Core features supported (no MCP)
- JS/Node: Core features supported (no MCP)
Examples
Run the example application:examples/simple-examples/src/main/kotlin/ai/koog/agents/example/ktor/
Next Steps
- Learn about Spring Boot integration for enterprise applications
- Explore Model Context Protocol for tool integration
- See Agent Client Protocol for bidirectional communication
- Check out streaming examples for real-time responses