8. Run Background Work
Background mode starts a response asynchronously and lets the application poll for terminal status.
8.1 Submit and Poll a Background Response
This cell sends background=true, stores the response ID, polls while status is queued or in progress, and then prints the final manager summary. Inspect the status history, final status, response ID, and token summary.
python
from __future__ import annotations
backlog = """
Same-day BrightCart support backlog:
1. 18 delayed-order contacts, mostly from the West Coast distribution lane.
2. 7 damaged-item replacement contacts; 3 mention replacement delays.
3. 5 return-window exception requests after holiday promotions.
""".strip()
background_payload = {
"model": MODEL_ID,
"input": f"Return exactly three labeled plain-text lines for a support-manager summary: theme, risk, next action. Keep each line under 12 words. Do not use leading hyphens or bold text.\n\n{backlog}",
"background": True,
"max_output_tokens": 1024,
"store": True,
}
print_request_shape(background_payload)
try:
background_response = create_response(**background_payload)
remember_stored_response(background_response)
status_history = [getattr(background_response, "status", None)]
for _ in range(15):
if getattr(background_response, "status", None) not in {"queued", "in_progress"}:
break
time.sleep(2)
background_response = retrieve_response(background_response.id)
status_history.append(getattr(background_response, "status", None))
background_summary = summarize_response(background_response)
manager_summary = output_text(background_response).strip()
require(manager_summary, "Background response did not return text.")
status = "pass" if background_summary.get("status") in {None, "completed"} else "warn"
record_check("Background mode", status, {"status_history": status_history, "id": getattr(background_response, "id", None), "final_status": background_summary.get("status")})
record_response("Background manager summary", "text", manager_summary)
print_labeled_json("Result: status history", status_history)
print_labeled_text("Result: manager summary", manager_summary)
print_response_summary(background_summary)
print_key_takeaway('Background mode starts work asynchronously and lets the application poll by response ID.')
except Exception as exc:
handle_example_error("Background mode", exc)Request shape
| field | value |
|---|---|
| model | openai.gpt-5.4 |
| max_output_tokens | 1024 |
| store | True |
| background | True |
| input | Return exactly three labeled plain-text lines for a support-manager summary: theme, risk, next action. Keep each line under 12 words. Do not use leading hyphens or bold text. Same-day BrightCart support backlog: 1. 18 delayed-order contacts, mostly from the We... |
Result: status history
[ "in_progress", "completed" ]
Result: manager summary
theme: Shipping delays dominate, especially West Coast distribution lane. risk: Rising dissatisfaction from delays, replacements, and return exceptions. next action: Escalate West Coast lane issues and review holiday return policy.
Response summary
| field | value |
|---|---|
| id | resp_lmmtsvgk3ntolh5ci5vxccmsa6uxcgrsq7v54jpz7oewmociyesa |
| model | openai.gpt-5.4 |
| status | completed |
| output_item_types | ['message'] |
| input_tokens | 246 |
| cached_input_tokens | 0 |
| output_tokens | 45 |
| total_tokens | 291 |
| reasoning_output_tokens | 0 |
| service_tier | default |
Key takeaway: Background mode starts work asynchronously and lets the application poll by response ID.