Skip to content

Support for Long running / Background Agents #2111

Open
@vikigenius

Description

@vikigenius

Description

Extracted from this issue #2086 since this is more generic

OpenAI has a background mode that works something like this

from openai import OpenAI
from time import sleep

client = OpenAI()

resp = client.responses.create(
  model="o3",
  input="Write a very long novel about otters in space.",
  background=True,
)

while resp.status in {"queued", "in_progress"}:
  print(f"Current status: {resp.status}")
  sleep(2)
  resp = client.responses.retrieve(resp.id)

print(f"Final status: {resp.status}\nOutput:\n{resp.output_text}")

Since Agents can also be really long running. We can think of running Agents in the background.

I was thinking of adding a new API

agent = Agent(...)
resp = agent.start()   # similar API to run, but this runs the agent as a background task

resp can still be an instance of AgentRun, and we can support the idea of background tasks in AgentRun itself and add a status property on it.

You would then check the status of the agent run using.

resp.status

The agent.run can then be thought of as just waiting for the agent to finish.

Implementation Thoughts

I am still not too sure on the implementation details, but most likely this might end up with pydantic_ai having some kind of task queue/system where agents can run in background.

References

https://platform.openai.com/docs/guides/background

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions