Core¶
The core module wires an agent callable to Redis-backed durable tasks and background workers.
AgentKit¶
AgentKit
¶
AgentKit(redis_url: str | None = None, *, redis_client: Redis | None = None, prefix: str | None = None, agent_callable: AgentCallable | ContextAgentCallable | None = None, middleware: list[TaskMiddleware] | None = None, queue_name: str | None = None, settings: Settings | None = None, auto_result_middleware: bool = True, enable_sse: bool = False, enable_streaming: bool = False, stream_config: StreamConfig | None = None, subtask_config: SubTaskConfig | None = None)
Convenience class that bundles TaskManager, Memory, and helpers.
This is the main entry point for using Redis Agent Kit in your application. Docket integration is internal - users only interact with tasks and memory.
Example
async def my_agent(ctx: TaskContext) -> dict: # ctx.memory is always available await ctx.memory.add_message("assistant", "Processing...") return {"response": f"Processed: {ctx.message}"}
kit = AgentKit("redis://localhost:6379", agent_callable=my_agent)
Example (with middleware): from redis_agent_kit.middleware import RAGMiddleware, MemoryMiddleware
async def my_agent(ctx: TaskContext) -> dict:
# ctx.rag_context is injected by RAGMiddleware
return {"response": f"Based on context: {ctx.rag_context}"}
kit = AgentKit(
"redis://localhost:6379",
agent_callable=my_agent,
middleware=[RAGMiddleware(), MemoryMiddleware()],
)
Initialize AgentKit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_url
|
str | None
|
Redis URL (e.g., "redis://localhost:6379"). Preferred. |
None
|
redis_client
|
Redis | None
|
Optional pre-created async Redis client. If provided, redis_url is still needed for background workers. |
None
|
prefix
|
str | None
|
Optional custom key prefix (default: 'rak') |
None
|
agent_callable
|
AgentCallable | ContextAgentCallable | None
|
Your agent function. Can be either: - (task_id, session_id, message, context) -> dict - (ctx: TaskContext) -> dict (use with middleware) |
None
|
middleware
|
list[TaskMiddleware] | None
|
List of TaskMiddleware to apply (in order) |
None
|
queue_name
|
str | None
|
Background worker queue name (default from settings) |
None
|
settings
|
Settings | None
|
Optional Settings instance (uses global settings if not provided) |
None
|
auto_result_middleware
|
bool
|
Automatically include ResultMiddleware (default: True). Set to False if you want full control over middleware. |
True
|
enable_sse
|
bool
|
Enable Pub/Sub publishing for SSE streaming (default: False).
Deprecated – use |
False
|
enable_streaming
|
bool
|
Enable lightweight Pub/Sub publishing for
token-by-token streaming (default: False).
Deprecated – use |
False
|
stream_config
|
StreamConfig | None
|
Structured streaming configuration. When provided,
|
None
|
Example
Simple - just URL¶
kit = AgentKit("redis://localhost:6379", agent_callable=my_agent)
With existing client¶
client = redis.from_url("redis://localhost:6379") kit = AgentKit("redis://localhost:6379", redis_client=client, ...)
With StreamConfig¶
kit = AgentKit( "redis://localhost:6379", stream_config=StreamConfig(enabled=True, channels={ChannelScope.TASK, ChannelScope.SESSION}), )
Source code in redis_agent_kit/core.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
worker_task
property
¶
Get the worker task handler for use with Docket.
This is the public API for getting a handler suitable for background workers. Use this instead of _create_wrapped_handler().
Example
kit = AgentKit("redis://localhost:6379", agent_callable=my_agent)
For Docket workers:¶
tasks = [kit.worker_task]
Returns:
| Type | Description |
|---|---|
Callable[[str, str, str, dict[str, Any]], Coroutine[Any, Any, dict[str, Any]]]
|
Wrapped async function suitable for Docket workers |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no agent_callable is configured |
create_task
async
¶
create_task(message: str, context: dict[str, Any] | None = None, session_id: str | None = None, user_id: str | None = None) -> tuple[TaskState, str]
Create a new task with a memory session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The initial user message |
required |
context
|
dict[str, Any] | None
|
Optional context data |
None
|
session_id
|
str | None
|
Optional existing session ID |
None
|
user_id
|
str | None
|
Optional user ID for memory isolation |
None
|
Returns:
| Type | Description |
|---|---|
tuple[TaskState, str]
|
Tuple of (TaskState, session_id) |
Source code in redis_agent_kit/core.py
create_and_submit_task
async
¶
create_and_submit_task(message: str, context: dict[str, Any] | None = None, session_id: str | None = None, caller: Caller | None = None, user_id: str | None = None, attachments: list[Any] | None = None) -> dict[str, Any]
Create a task and submit for background processing via Docket.
This is the primary method for creating tasks that will be processed asynchronously. Docket handles the background execution - users only see tasks and memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The user message/query |
required |
context
|
dict[str, Any] | None
|
Optional context data (user_id, instance_id, etc.) |
None
|
session_id
|
str | None
|
Optional existing session ID |
None
|
caller
|
Caller | None
|
Optional caller identity (who is creating this task) |
None
|
user_id
|
str | None
|
Optional user ID for memory isolation |
None
|
attachments
|
list[Any] | None
|
Optional list of Attachment objects for multi-modal input |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with task_id, session_id, and status |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no agent_callable is configured |
Source code in redis_agent_kit/core.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
get_emitter
¶
get_emitter(task_id: str) -> TaskEmitter
Get a TaskEmitter for a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task ID |
required |
Returns:
| Type | Description |
|---|---|
TaskEmitter
|
TaskEmitter for emitting progress updates |
cancel_task
async
¶
Cancel a task, optionally cascading to descendants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task ID to cancel. |
required |
cascade
|
bool | None
|
When True, cancels all descendant tasks. Defaults to the
kit's |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of tasks transitioned to CANCELLED. |
Source code in redis_agent_kit/core.py
submit_input
async
¶
Submit user input to a task that is awaiting input.
This stores the user's response and re-queues the task for processing. The task handler will receive the input in the task's input_response field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task ID |
required |
response
|
dict[str, Any]
|
Dictionary mapping field names to values. For simple prompts without fields, use {"response": "..."} |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if input was submitted successfully, False if task not found |
bool
|
or not in AWAITING_INPUT status. |
Example
Simple text response¶
await kit.submit_input(task_id, {"response": "Yes, proceed"})
Structured response matching schema¶
await kit.submit_input(task_id, { "database": "Redis", "confirm": True, })
Source code in redis_agent_kit/core.py
create_task_with_session¶
create_task_with_session
async
¶
create_task_with_session(task_manager: TaskManager, memory: Memory, message: str, context: dict[str, Any] | None = None, session_id: str | None = None, user_id: str | None = None) -> tuple[TaskState, str]
Create a new task with a memory session.
This is the primary entry point for starting a new conversation. It creates a memory session (if needed), adds the initial message, and creates a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_manager
|
TaskManager
|
The TaskManager instance |
required |
memory
|
Memory
|
The Memory instance |
required |
message
|
str
|
The initial user message |
required |
context
|
dict[str, Any] | None
|
Optional context data (user info, instance IDs, etc.) |
None
|
session_id
|
str | None
|
Optional existing session ID |
None
|
user_id
|
str | None
|
Optional user ID for memory isolation |
None
|
Returns:
| Type | Description |
|---|---|
tuple[TaskState, str]
|
Tuple of (TaskState, session_id) |
Source code in redis_agent_kit/core.py
RetryConfig¶
RetryConfig
dataclass
¶
Configuration for task retry behavior.
ConcurrencyConfig¶
ConcurrencyConfig
dataclass
¶
Configuration for task concurrency limits.
Settings¶
Settings
¶
Bases: BaseSettings
Redis Agent Kit configuration.
Example usage
from redis_agent_kit import Settings, AgentKit
Use defaults¶
kit = AgentKit()
Use config file¶
export RAK_CONFIG=/path/to/config.yaml¶
kit = AgentKit()
Programmatic¶
settings = Settings(redis_url="redis://prod:6379") kit = AgentKit(settings=settings)