The async job pattern
Instead of holding the connection open, your agent should:- Accept the request and immediately return a
job_id. - Run the task in the background.
- Let the caller poll a status endpoint until the result is ready.
Implementation
Python (FastAPI)
Node.js (Express)
Calling the endpoint
Persisting job state
GMI containers are stateless. If a container restarts, any in-memory job state is lost. For production, write the job state to an external store such as Redis or a database, and inject the connection credentials as Secrets in Step 4 of Register an agent.Checklist
Before deploying an agent that handles long-running tasks:- Accept requests with a 202 response and a
job_id - Run the actual work in a background task, not the request handler
- Expose a
GET /jobs/:idendpoint for polling - Persist job state to Redis or a database, not in-memory
- Set appropriate poll intervals (3-5 seconds) to avoid hammering the endpoint
- Return clear error states (
failed,timeout) so callers can handle them