For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Agents API

Send tasks and handle tool calls

To give an agent a task, send a message to a session. If the session is idle, the message starts a turn, one round of work. If a turn is already running, the agent uses the new message to adjust the work in progress.

The turn runs in the background. Your app can open an event stream, a connection over which the API sends updates as work happens. One of those updates can be a request to run a function tool, a function in your app that the agent can call.

Step 1 of 12Subscribe to session eventsHTTP request from Your app to OpenAI Agents API.
Your app
Opens an event stream for the authorized session.
HTTP request
OpenAI Agents API
Sends updates over this connection.

Subscribe to session events

HTTP request · GET /v1/agents/sessions/sess_a/events

Messages, function calls, and results

Your app uses /v1/agents/sessions/{session_id}/events in both directions. A GET opens the stream. Separate POST requests send messages and tool results.

  • A message. Open the stream before posting agent.session.input.message so you receive the first updates. An idempotency key is a unique ID that lets the API recognize a retry. Save it with the message and send it in the Idempotency-Key header. If you miss the HTTP response, resend the same message and key to the same session.
  • A request for action. The event agent.session.requires_action tells you the agent needs something from your app. For a function call, session.required_actions lists the function name and arguments. Your app checks permissions and arguments before running the function.
  • A function result. Send the output or error as agent.session.input.tool_result. The turn_id and call_id tell the API which pending call the result answers. Once all required results arrive, the turn can continue.

HTTP 202 confirms that the API accepted your input. The turn events report the outcome later. The turn’s final status can be completed, failed, or cancelled. If the stream disconnects, retrieve the saved work and pending actions. The stream doesn’t replay missed events.

Example: Find the request behind a finance report

Herringbone’s app sends an agent.session.input.message event to Customer A’s session (sess_a). In the diagram, the message with role: "user" contains the task from Herringbone. The agent requests get_integration_context to see how the job fetches payments. The function takes no arguments. Herringbone uses sess_a to identify the integration and checks that Customer A has authorized access to its records.

The lookup finds the request to Herringbone’s payments API, GET /v1/payments?limit=100, in exports/nightly.http. It also returns the column names finance expects, payment_id and amount. Herringbone saves the result and posts it to sess_a with turn_id set to turn_plan and call_id set to call_context. These IDs match the pending call in the diagram.

The integration notes show that the job makes just one request each night. In its reply, the agent recommends reading next_cursor after processing each page. If it has a value, the job should send it back as the cursor parameter on the next request. It should repeat until there’s no next cursor.

Saving the function result lets Herringbone resend it after a disconnect if the call is still pending. This matters especially for functions that write data. If a write may have succeeded but there’s no saved result, check what changed before running it again.

These Python excerpts follow one turn of the review. Full program combines them in one runnable file.

Review Customer A’s nightly export

Here, sessions is OpenAI().beta.agents.sessions. Herringbone opens the event stream before sending the message. When the session needs a function result, the loop passes each pending call to answer_call and keeps listening until the turn ends.

Send the review task
def review_export(sessions, session_id, authorized_exports):
    task = """Herringbone now returns payments one page at a time.
Send next_cursor back as cursor to fetch the next page.
Review Customer A's nightly export. What should the customer change?"""
    message = {
        "type": "agent.session.input.message",
        "input": [{"role": "user", "content": [{"type": "input_text", "text": task}]}],
    }
    key = str(uuid4())
    results = {}
    with sessions.events.stream(session_id) as events:
        sessions.events.create(session_id, events=[message], idempotency_key=key)
        for event in events:
            if event.type == "agent.session.requires_action":
                for call in event.session.required_actions:
                    answer_call(sessions, session_id, call, authorized_exports, results)
            elif event.type == "agent.session.turn.completed":
                return event.turn.id
            elif event.type in {
                "agent.session.turn.failed",
                "agent.session.turn.cancelled",
                "agent.session.failed",
                "error",
            }:
                raise RuntimeError(event.type)
    raise RuntimeError("The stream closed before the turn finished.")
Ask your coding agent
When Herringbone changes its API, send a review task to each authorized customer’s Agents API session. Ask how the change affects that integration and what the customer should update. Subscribe to events first and save each message and its idempotency key. On a pending get_integration_context call, check which records the customer authorized the review to read. Run the lookup and return the result with the matching turn and call IDs. Save each result by session, turn, and call ID so you can resend it after a disconnect. Report each turn’s outcome. Read this lesson for context: https://developers.openai.com/api/training/agents-api/3-send-tasks-and-handle-tool-calls.md