Open MercatoDocsSearch docsAPI reference

Deployment

Agent Orchestration Scaling

Size the workers, queue and database as one system. A higher concurrency setting can move the bottleneck to the next stage of an agent run.

This runbook explains how to configure an Open Mercato deployment so the agent-orchestration pipeline (the enterprise agent_orchestrator module plus the core workflows engine) sustains real production load. Out of the box the platform is tuned for development, not for a fleet of LLM-bound agent runs.

Why the defaults are not enough

The reference load target is 1,000 cases/day × ~12 agents per case ≈ 12,000 agent runs/day. With an average run taking on the order of a minute, that works out to roughly ~20 concurrently executing LLM-bound runs in steady state across a business-hours window (and 50–100 at peak). The default configuration – a file-based sequential queue processed by a single process at concurrency 1 – is an order of magnitude below that.

Three things must change for production agent load: the queue strategy, the worker deployment, and (in containerized topologies) the OpenCode URL.

Queue strategy: QUEUE_STRATEGY=async is required

QUEUE_STRATEGY=async
REDIS_URL=redis://your-redis:6379

The default local queue strategy (packages/queue/src/strategies/local.ts) stores jobs as JSON files under .mercato/queue/<name>/ and processes them sequentially – the concurrency option is accepted for logging/compatibility only, and the strategy is explicitly not suitable for production or multi-process environments. A minute-long agent run therefore blocks every other job behind it, capping throughput near 1,400 one-minute runs/day no matter how much hardware you add.

QUEUE_STRATEGY=async switches to the Redis-backed BullMQ strategy, which supports real per-queue concurrency, retries with backoff, and multiple worker processes consuming the same queue.

Run a dedicated worker fleet

Do not rely on AUTO_SPAWN_WORKERS in the web process for agent load – that couples LLM-bound job execution to the web process's lifecycle, memory, and DB pool. Deploy dedicated worker processes instead:

# All discovered queues in one process:
yarn mercato queue worker --all

# Or one process per queue (preferred for isolating agent load):
yarn mercato queue worker workflow-invoke-agent
yarn mercato queue worker workflow-activities

Sizing formula

runs/day × avg run seconds ÷ business-window seconds  ⇒  required concurrent slots

Worked example for the reference load: 12,000 runs/day × 60 s ≈ 720,000 busy-seconds/day. Spread over an 8-hour business window (28,800 s) that is ~25 slots, so plan for ~20–30 concurrent slots – for example 3 worker replicas × concurrency 10. Runs are LLM-bound (I/O-wait), so a single Node process handles concurrency 10 comfortably; scale out with replicas rather than pushing per-process concurrency past 20 (the queue package's hard guideline).

Concurrency tuning knobs

Env varDefaultQueueMeaning
WORKERS_WORKFLOW_INVOKE_AGENT_CONCURRENCY5workflow-invoke-agentPer-process concurrency of the dedicated agent-job worker. This is the primary throughput knob for agent runs.
WORKERS_WORKFLOW_ACTIVITIES_CONCURRENCY1workflow-activitiesPer-process concurrency of the general workflow-activities worker (timers, emails, API calls, sub-workflow resume).

INVOKE_AGENT jobs run on the dedicated workflow-invoke-agent queue so minute-long LLM runs cannot starve the fast housekeeping activities on workflow-activities. Tune the two independently; there is rarely a reason to raise the activities concurrency for agent load.

DB connection budget

Each in-flight worker job builds its own request container and checks out one pooled DB connection for the job's duration, so a worker process's peak connection demand is Σ(per-queue concurrency). At startup, mercato queue worker fits that sum to a connection budget:

Always verify the effective (post-clamp) concurrency. The worker logs the resolved plan at startup ([worker] DB connection budget: … and a clamp warning when it reduced anything). A silent clamp is the most common reason a fleet sized on paper for 30 slots actually runs 10. Keep the global invariant: web_pool_max + worker_pool_max + overhead ≤ Postgres max_connections, or front Postgres with pgbouncer.

Runtime protection

The agent runtime enforces process-local protection around each run:

Env varDefaultMeaning
OM_AGENT_RUN_TIMEOUT_MS300000Wall-clock deadline for one in-process agent run; on expiry the run is failed (no orphaned running row).
OM_AGENT_MAX_CONCURRENT_RUNS25Process-local cap on concurrently executing top-level agent runs.
OM_AGENT_MAX_CONCURRENT_RUNS_PER_TENANT10Per-tenant slice of the same admission gate.
OM_AGENT_ADMISSION_MAX_WAIT_MS30000Max bounded wait for a slot before the run is rejected with a capacity error.
OM_AGENT_ADMISSION_MAX_QUEUE100Max waiters per process; beyond this, rejection is immediate.

Warning · The admission cap is process-local, not fleet-wide

OM_AGENT_MAX_CONCURRENT_RUNS bounds one process. The fleet-wide bound is cap × replicas – with 4 worker replicas at the default cap of 25, up to 100 runs can execute cluster-wide. The fleet-wide throttle is worker concurrency (WORKERS_WORKFLOW_INVOKE_AGENT_CONCURRENCY × replicas); the admission gate only protects each process's DB pool and the OpenCode service from direct-caller bursts (playground, scripts, agent fan-out).

Provider LLM budgets (native runtime)

Native runs (runtime: 'native', the in-process engine – historical rows may carry the legacy 'in-process' label; treat both as one cohort) wrap every model call in a process-local per-provider budget: a concurrency semaphore keyed by the resolved provider id, plus automatic retry on 429/overloaded responses with jittered exponential backoff, always bounded by the run's own OM_AGENT_RUN_TIMEOUT_MS deadline. Budget exhaustion surfaces as the same retryable capacity error as the admission gate, so queued jobs retry and the playground returns 429 with Retry-After.

Env varDefaultMeaning
OM_AGENT_PROVIDER_MAX_CONCURRENT10Per-provider process-local cap on concurrent LLM calls (all providers).
OM_AGENT_PROVIDER_MAX_CONCURRENT_<PROVIDER> – Per-provider override, uppercased id (e.g. OM_AGENT_PROVIDER_MAX_CONCURRENT_ANTHROPIC).
OM_AGENT_PROVIDER_RETRY_MAX4Max retries on 429/overloaded before surfacing a retryable capacity failure.
OM_AGENT_PROVIDER_RETRY_BASE_MS1000Backoff base (exponential + full jitter), bounded by the run deadline.
OM_AGENT_TRACE_CAPTUREonEscape hatch (off) for the native runtime's always-on per-step span capture.

Like the admission gate, the provider budget is process-local: the aggregate pressure on a provider is budget × replicas. Size it so OM_AGENT_PROVIDER_MAX_CONCURRENT × worker replicas stays inside your provider's rate tier, and treat a sustained retry rate as the signal to lower it (or raise the provider tier) rather than relying on backoff.

Sizing for hundreds of parallel runs (native runtime)

Native runs are LLM-latency-bound I/O waits – a worker at concurrency K holds K pending promises at negligible CPU cost, so "hundreds in parallel" is a fleet configuration, not an engine limit. Worked example for 100 concurrent runs:

OpenCode connectivity

Env varDefaultMeaning
OPENCODE_URLhttp://localhost:4096Base URL of the OpenCode server. In containerized topologies this must point at the OpenCode service name – the compose bundles use http://opencode:4096. The localhost default is unreachable from inside an app or worker container.
OM_OPENCODE_RUN_TIMEOUT_MS300000Wall-clock ceiling for one OpenCode-executed agent run.

Index pre-creation on large tables

The agent-orchestrator performance migration adds composite indexes on agent_runs and agent_proposals using CREATE INDEX IF NOT EXISTS. A plain CREATE INDEX blocks writes for the duration of the build, so on deployments where those tables are already large, pre-create the indexes with CREATE INDEX CONCURRENTLY before deploying – the migration then becomes a no-op thanks to IF NOT EXISTS.

ACL sync after upgrade (run-list access)

GET /api/agent_orchestrator/runs is gated on the agent_orchestrator.trace.view feature (previously agent_orchestrator.agents.view). New tenants receive the grant automatically from setup.ts defaultRoleFeatures, but existing tenants provisioned before this change do not – their operators will get a 403 on the run list until the feature is re-applied.

After deploying, run:

yarn mercato auth sync-role-acls

This re-applies defaultRoleFeatures to every existing tenant's roles, restoring run-list access for every persona that already had agents.view (the two features are granted together, so the sync is deterministic – see runs-acl-rollout.test.ts). The command is idempotent; re-running it is safe.

Monitoring checklist

Watch these signals in production:

Next: Continuous Integration with GitHub Actions