import asyncio
from afk.llms import LLMBuilder
from afk.llms.middleware import MiddlewareStack
from afk.llms.middleware.timeout import (
TimeoutMiddleware,
TimeoutConfig,
)
from afk.llms.cache.redis_pool import (
get_redis_pool,
PoolConfig,
close_all_pools,
)
from afk.memory.adapters.redis import RedisMemoryStore
from afk.core import Runner
from afk.agents import Agent
class ProductionSetup:
def __init__(self):
self.llm_client = None
self.runner = None
self.pool = None
async def __aenter__(self):
pool_config = PoolConfig(
max_connections=50,
max_idle_connections=10,
socket_timeout=5.0,
socket_connect_timeout=5.0,
)
self.pool = await get_redis_pool(
"redis://localhost:6379/0",
config=pool_config,
)
timeout_config = TimeoutConfig(
default_timeout_s=30.0,
chat_timeout_s=60.0,
)
stack = MiddlewareStack(
chat=[TimeoutMiddleware(timeout_config)],
)
self.llm_client = (
LLMBuilder()
.provider("openai")
.model("gpt-5.5")
.profile("production")
.with_middlewares(stack)
.build()
)
self.runner = Runner(
memory_store=RedisMemoryStore(url="redis://localhost:6379/0"),
)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.runner:
await self.runner.close()
await close_all_pools()
return False
async def main():
agent = Agent(
name="assistant",
model="gpt-5.5",
instructions="You are a helpful assistant.",
)
async with ProductionSetup() as setup:
result = await setup.runner.run(
agent,
user_message="Hello, world!",
)
print(result.final_text)
asyncio.run(main())