showcase/shell-docs/src/content/ag-ui/sdk/kotlin/client/overview.mdx
The kotlin-client module provides high-level agent implementations and client infrastructure for connecting to AI agents via the AG-UI protocol. It offers both stateless and stateful client options, authentication management, and tool integration capabilities.
dependencies {
implementation("com.agui:kotlin-client:0.2.1")
}
The client module automatically includes kotlin-core and kotlin-tools as dependencies.
Stateless client for cases where no ongoing context is needed or the agent manages all state server-side.
Stateful client that maintains conversation history and sends it with each request.
Learn more about StatefulAgUiAgent →
Low-level HTTP transport implementation providing direct protocol access.
Base class for implementing custom agent connectivity patterns.
Learn more about AbstractAgent →
Multiple authentication methods supported:
Real-time event streaming using Kotlin Flows:
TEXT_MESSAGE_CHUNK / TOOL_CALL_CHUNK events into start/content/end triadsAgentState.thinkingComprehensive state synchronization:
ToolMessage entries without additional wiringAgentState.rawEvents and AgentState.customEventsAgentState.thinkingClient-side tool execution framework:
Robust error management:
| Platform | Ktor Client Engine | Status |
|---|---|---|
| Android | ktor-client-android | ✅ Stable |
| iOS | ktor-client-darwin | ✅ Stable |
| JVM | ktor-client-cio | ✅ Stable |
val agent = AgUiAgent("https://api.example.com/agent") {
bearerToken = "your-token"
}
agent.sendMessage("Hello!").collect { state ->
println("Response: ${state.messages.last()}")
}
agent.sendMessage("Plan the next steps").collect { state ->
state.thinking?.let { thinking ->
if (thinking.isThinking) {
val thought = thinking.messages.lastOrNull().orEmpty()
println("🤔 Agent thinking: $thought")
} else if (thinking.messages.isNotEmpty()) {
println("💡 Agent finished thinking: ${thinking.messages.joinToString()}")
}
}
}
The SDK provides convenience builders for common configurations:
// Quick bearer token setup
val agent = agentWithBearer("https://api.example.com/agent", "your-token")
// Quick API key setup
val agent = agentWithApiKey("https://api.example.com/agent", "your-api-key")
// Agent with debug logging
val agent = debugAgent("https://api.example.com/agent") {
bearerToken = "your-token"
}
val chatAgent = StatefulAgUiAgent("https://api.example.com/agent") {
apiKey = "your-api-key"
systemPrompt = "You are a helpful assistant"
}
// Conversation context is maintained automatically
chatAgent.chat("My name is Alice").collect { }
chatAgent.chat("What's my name?").collect { state ->
// Agent knows the name from previous message
state.customEvents?.forEach { custom ->
println("Custom event ${custom.name}: ${custom.value}")
}
state.rawEvents?.forEach { raw ->
println("Raw payload: ${raw.event}")
}
}
val agent = AgUiAgent("https://api.example.com/agent") {
customAuth { request ->
request.headers.append("X-Custom-Auth", "custom-value")
}
}