Example demonstrating background execution with polling and cancellation.
Background execution allows you to start an agent run that returns immediately.
Background execution allows you to start an agent run that returns immediately with a PENDING status, while the actual work continues in the background. You can then poll for completion or cancel the run.
1"""2Example demonstrating background execution with polling and cancellation.34Background execution allows you to start an agent run that returns immediately5with a PENDING status, while the actual work continues in the background.6You can then poll for completion or cancel the run.78Requirements:9- PostgreSQL running (./cookbook/scripts/run_pgvector.sh)10- OPENAI_API_KEY set1112Usage:13 .venvs/demo/bin/python cookbook/02_agents/other/background_execution.py14"""1516import asyncio1718from kern.agent import Agent19from kern.db.postgres import PostgresDb20from kern.models.openai import OpenAIResponses21from kern.run.base import RunStatus2223# ---------------------------------------------------------------------------24# Config25# ---------------------------------------------------------------------------2627db = PostgresDb(28 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",29 session_table="background_exec_sessions",30)313233# ---------------------------------------------------------------------------34# Create and Run Background Examples35# ---------------------------------------------------------------------------363738async def example_background_run_with_polling():39 """Start a background run and poll until complete."""40 print("=" * 60)41 print("Example 1: Background run with polling")42 print("=" * 60)4344 agent = Agent(45 name="BackgroundAgent",46 model=OpenAIResponses(id="gpt-5-mini"),47 description="An agent that runs in the background",48 db=db,49 )5051 # Start a background run — returns immediately with PENDING status52 run_output = await agent.arun(53 "What is the capital of France? Answer in one sentence.",54 background=True,55 )5657 print(f"Run ID: {run_output.run_id}")58 print(f"Session ID: {run_output.session_id}")59 print(f"Status: {run_output.status}")60 assert run_output.status == RunStatus.pending, (61 f"Expected PENDING, got {run_output.status}"62 )6364 # Poll for completion65 print("\nPolling for completion...")66 for i in range(30):67 await asyncio.sleep(1)68 result = await agent.aget_run_output(69 run_id=run_output.run_id,70 session_id=run_output.session_id,71 )72 if result is None:73 print(f" [{i + 1}s] Run not found in DB yet")74 continue7576 print(f" [{i + 1}s] Status: {result.status}")7778 if result.status == RunStatus.completed:79 print(f"\nCompleted! Content: {result.content}")80 break81 elif result.status == RunStatus.error:82 print(f"\nFailed! Content: {result.content}")83 break84 else:85 print("\nTimed out waiting for completion")8687 print()888990async def example_cancel_background_run():91 """Start a background run and cancel it before completion."""92 print("=" * 60)93 print("Example 2: Cancel a background run")94 print("=" * 60)9596 agent = Agent(97 name="CancellableAgent",98 model=OpenAIResponses(id="gpt-5-mini"),99 description="An agent whose run can be cancelled",100 db=db,101 )102103 # Start a long background run104 run_output = await agent.arun(105 "Write a very detailed essay about the history of computing. "106 "Make it at least 5000 words with sections and subsections.",107 background=True,108 )109110 print(f"Run ID: {run_output.run_id}")111 print(f"Status: {run_output.status}")112113 # Wait a moment for the run to start114 await asyncio.sleep(2)115116 # Cancel the run117 print("Cancelling run...")118 cancelled = await agent.acancel_run(run_id=run_output.run_id)119 print(f"Cancel result: {cancelled}")120121 # Check the final state122 await asyncio.sleep(1)123 result = await agent.aget_run_output(124 run_id=run_output.run_id,125 session_id=run_output.session_id,126 )127 if result:128 print(f"Final status: {result.status}")129 print()130131132async def example_cancel_before_start():133 """Cancel a run before it even starts (cancel-before-start semantics)."""134 print("=" * 60)135 print("Example 3: Cancel-before-start")136 print("=" * 60)137138 from kern.run.cancel import cancel_run139140 agent = Agent(141 name="PreCancelAgent",142 model=OpenAIResponses(id="gpt-5-mini"),143 description="An agent whose run is cancelled before starting",144 db=db,145 )146147 # Pre-generate a run ID148 from uuid import uuid4149150 run_id = str(uuid4())151152 # Cancel the run BEFORE it starts153 print(f"Pre-cancelling run {run_id}...")154 cancel_run(run_id)155156 # Now start the run with that ID — it should detect the cancellation157 run_output = await agent.arun(158 "This should be cancelled before it runs.",159 background=True,160 run_id=run_id,161 )162163 print(f"Run ID: {run_output.run_id}")164 print(f"Initial status: {run_output.status}")165166 # Wait and check — the background task should detect the cancellation167 await asyncio.sleep(2)168 result = await agent.aget_run_output(169 run_id=run_output.run_id,170 session_id=run_output.session_id,171 )172 if result:173 print(f"Final status: {result.status}")174 print()175176177async def main():178 await example_background_run_with_polling()179 await example_cancel_background_run()180 await example_cancel_before_start()181 print("All examples completed!")182183184if __name__ == "__main__":185 asyncio.run(main())Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89# Optiona: Run PgVector (needs docker)10./cookbook/scripts/run_pgvector.sh1112# Export relevant API keys13export OPENAI_API_KEY="***"1415python background_execution.py