Problem
BrokerService.invoke_agent() makes 4-6 sequential database round trips per invocation:
get_effective_config() (broker config)
get_by_id() (conversation validation)
get_agent_by_agent_id() (agent lookup)
get_agent_credential() (credential fetch)
count_invocations_for_integration() (daily quota — now Redis, but DB fallback)
count_invocations_for_integration() (monthly quota — same)
Each DB round trip adds 1-3ms with connection pooling. These are mostly independent and can run concurrently.
Proposed Solution
Group independent queries using asyncio.gather():
- Parallel group 1: agent lookup + config lookup + conversation lookup (if provided)
- Parallel group 2: daily quota + monthly quota (if Redis fallback)
This should reduce broker overhead by 30-50% per invocation.
Files
src/services/broker.py — main target
Problem
BrokerService.invoke_agent()makes 4-6 sequential database round trips per invocation:get_effective_config()(broker config)get_by_id()(conversation validation)get_agent_by_agent_id()(agent lookup)get_agent_credential()(credential fetch)count_invocations_for_integration()(daily quota — now Redis, but DB fallback)count_invocations_for_integration()(monthly quota — same)Each DB round trip adds 1-3ms with connection pooling. These are mostly independent and can run concurrently.
Proposed Solution
Group independent queries using
asyncio.gather():This should reduce broker overhead by 30-50% per invocation.
Files
src/services/broker.py— main target