# GPT-Live

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.

## 5. Built-in delegation with Responses

In this lesson, we’ll look at delegation, the second key idea behind GPT-Live. GPT-Live can keep talking with you while a backend handles more complex work, then share the result when it’s ready.

You can connect GPT-Live to a backend in two ways.

- With **built-in delegation**, GPT-Live handles the back and forth with Responses or Decisions. It automatically passes along context from the conversation and brings the result back into the session.
- With **client delegation**, your application handles the back and forth between GPT-Live and the backend. This is useful if you want to add voice to an agent you’ve already built or choose what context to send to the backend and what to pass back to GPT-Live.

Let’s start with Responses delegation.

In the example below, you’ll see how GPT-Live delegates to Responses so a shopping assistant can search the web for what the shopper needs.

Now let’s walk through how this works.



When our server creates a session, we set the delegation type to `responses` and choose the backend model and its tools. A tool can run on OpenAI’s servers or in our application. We’ll start with web search, which OpenAI hosts. As before, we tell GPT-Live when to delegate and put the detailed instructions in the backend prompt.







Our server sends the request to OpenAI to create the GPT-Live session and set up the Responses backend.







As the shopper and the assistant talk, GPT-Live automatically keeps track of the conversation.







When GPT-Live decides to delegate, it sends Responses the conversation so far and asks it to continue the task using the backend prompt.







When GPT-Live creates a delegation, the browser gets an event with the delegation ID. Each update from Responses arrives inside a `response.event` with that same ID, so we can trace the entire task.







Responses uses the conversation and its instructions to decide which tools to call. Here it chooses web search, and the highlighted event shows the queries it used. After reviewing the results, Responses can call a tool again if it needs more information.







Here, Responses has enough information to write an answer. You can see the text stream in along with links to its sources. The highlighted event contains the full answer.







The answer also goes back to GPT-Live automatically, so it can respond to the user out loud.















### Using a custom tool with Responses

Now let’s give Responses a custom tool that searches our store’s inventory instead of the web.



Let’s say we already have a tool on our server called `search_catalog`. It searches the store’s inventory by category and an optional price limit. Here, the inventory is a Markdown file. You can try the tool below and open `inventory.md` to see all the products.







As before, we set the delegation type to `responses` and choose the backend model. We add the function definition to `tools` so Responses knows what `search_catalog` does and which arguments it accepts.







Our server sends the request to OpenAI. OpenAI creates the GPT-Live session, sets up the Responses backend, and returns the session ID to our server.







In the web search example, events went to the browser. Our custom tool runs on the server, so it uses the session ID to request a sideband connection. OpenAI replies with `101 Switching Protocols`. The WebSocket is now open, and our server can receive tool calls directly.







As before, GPT-Live automatically keeps track of the conversation.







When GPT-Live decides to delegate, it passes the conversation so far to Responses.







Now our server gets the same updates as the browser. Over the sideband, it receives the delegation ID and the Responses events associated with it.







Here, Responses calls `search_catalog`. The highlighted event includes the arguments and a call ID that we’ll send back with the result.







Our server runs the tool with the arguments Responses supplied.







Our server sends the result back with the same call ID so Responses can match it to the tool call.







Responses can call multiple tools in parallel. Once our server has returned a result for every pending call, it asks Responses to continue. The highlighted event tells us the new response has started.







Responses uses the tool results to write its answer, and we can follow along as the text streams in.







As before, the answer goes back to GPT-Live automatically so it can respond out loud.










### Sharing application context with GPT Live

Let’s say we want users to keep browsing while they talk to GPT Live. Our application can share what they’re looking at so they can ask questions about the page.

The shopper asks, “Hi, would this travel case fit either of the cameras that we were looking at earlier, ideally also with the lens?” GPT-Live answers, “It’ll fit either camera body, but you’d need to remove the lens.”



Here’s how we can share application context with GPT Live.



When our server creates a new session, it can include a summary of what the user is looking at in `session.input`.







As the shopper browses, we can keep GPT Live up to date by sending page changes with `session.thinking.append`.







The browser can use `response.item.create` to make an image or screenshot available to a vision-capable Responses model. To have Responses analyze it right away, send `response.create`. You can also leave the image queued until GPT Live delegates a question about it.





### Try it yourself

Use the prompt below to build your own shopping assistant with Codex. Try the same request with each tool and follow the events to see how GPT-Live and Responses work together.



**Build the shopping assistant:**

```text
[@OpenAI Developers](plugin://openai-developers@openai-curated), build and run a voice shopping assistant with GPT-Live and built-in Responses delegation. Let me try the same shopping question first with web search, then with a custom tool that searches the store’s catalog. Show the actual events alongside the conversation so I can follow the handoff to Responses, see the tool call, and hear GPT-Live answer when the result comes back.

Lesson 5: https://developers.openai.com/api/training/gpt-live/5-built-in-delegation-with-responses.md
```
