OCI Responses API Compaction Tutorial
Use the OpenAI Python SDK with the OCI Responses API to compact conversation history explicitly or automatically, then continue the conversation with the returned context.
Overview
As a conversation grows, an application sends more history with each request. Compaction reduces that history to a smaller representation that the model can use in later turns. Use it to manage context in long-running conversations and agent workflows.
In this tutorial, you describe a fictional deployment configuration, compact the conversation, and ask a follow-up question to check which details the model retained.
You use the OpenAI Python SDK with an OCI Generative AI API key and a regional OCI endpoint. The SDK provides the client interface. Requests authenticate with OCI and are sent to OCI Generative AI.
All models supported by the OCI Responses API, including imported models, support both explicit and automatic compaction. Compaction is available in Enterprise AI regions and follows each model's region and hosting availability. Both modes are supported for on-demand inference and dedicated hosting.
| Mode | Operation | What to Send Next |
|---|---|---|
| Explicit (standalone) | Call client.responses.compact(...), which sends POST /responses/compact, when an application is ready to compact history. |
Pass the complete returned output array, followed by the next user message. |
| Automatic (server-managed) | Call client.responses.create(...) with context_management and a token threshold. OCI compacts context during POST /responses when the threshold is reached. |
With store=False, pass the latest compaction item and all the next output items, followed by the next user message. If no compaction item is returned, retain the existing history and append the new output. |
Compaction items contain opaque, encrypted context. Preserve their serialized fields and order. Don't read, edit, or replace their contents with a text summary.
Compaction consumes model tokens and can lose details during summarization. Compact before reaching the model's context limit, and leave room for the response and later turns. Explicit compaction is synchronous and doesn't support storage, background execution, or streaming.
The examples manage history in the application and set store=False on response-creation requests. For the separate workflow that uses OCI-managed conversation state, see Use Agent Memory with Responses API.
Before You Begin
To complete this tutorial, you need:
- Access to the OCI Responses API in a supported Enterprise AI region. Select a model from Supported Models and Regions that's available to your tenancy in the selected region. For foundational models, check Models by Region for on-demand and dedicated hosting availability.
- An OCI Generative AI API key with access to the selected model and project. If needed, create an API key and add its permissions.
- An OCI Generative AI project OCID. Compaction requires project context for every model. If needed, create a project.
- Python 3.10 or later on macOS, Linux, or Windows Subsystem for Linux. The commands use a Bash-compatible shell.
The examples use the OpenAI Python SDK with OCI credentials and an OCI base URL. They don't require an OpenAI API key, the OCI Python SDK, or the OCI CLI.
These examples use OCI Generative AI API key authentication. Compaction also supports OCI IAM authentication. For that setup, see Generative AI IAM-Based Authentication. An OCI Generative AI project OCID is required with either authentication method.
1. Prepare the Python Environment and OCI Access
2. Compact a Conversation Explicitly
This example creates a response, adds the complete response output to the history, and calls responses.compact. It then replaces the history with the complete compacted output and asks about the deployment's backup retention and maintenance window.
The short conversation makes the sequence easy to follow. In an application, call compaction when the accumulated history needs to be reduced and still fits within the model's context limit.
3. Enable Automatic Compaction
For automatic compaction, include context_management in a response-creation request with type: "compaction" and an explicit compact_threshold of at least 1000 tokens. Select a threshold that's less than the model's context limit and leave room for output and later turns.
This example creates 40 synthetic status exchanges locally to build a longer history. It sends that history in one request with a 1,000-token threshold, checks for compaction, and sends a follow-up request. The 40 exchanges don't make separate API calls.
4. Use a Model Hosted on a Dedicated AI Cluster (Optional)
You can use models that support the OCI Responses API for inference on demand or host them on dedicated AI clusters, according to each model's hosting options. Both compaction modes work with either hosting option. The earlier examples use an on-demand model ID. When you host a model on a dedicated AI cluster, you create a model endpoint on that cluster to call the model. Pass that endpoint's OCID as the request's model value.
The hosting cluster provides the Compute resources for inference. You can create several endpoints for the same hosted model, for example, to give different applications, teams, or cost centers their own access points. Each endpoint has its own OCID, and the endpoints share the cluster's Compute resources. For more information, see Managing Model Endpoints.
Check the Results
A returned compaction item confirms that compaction occurred. It doesn't establish that every detail was preserved. Review the follow-up answer against the original configuration.
To evaluate compaction for an application, use representative conversations with facts and instructions that matter to the workflow. Compare the same follow-up question with the full history, with explicit compaction, and with automatic compaction. Check retained facts, instruction following, output format, and any tool calls the workflow requires.
Run several examples and adjust the compaction threshold based on what you observe. Compaction itself uses model tokens, so a smaller context doesn't guarantee less total token usage or faster responses for every request.
Troubleshooting
| Symptom | What to Check |
|---|---|
| A script reports a missing environment variable. | Set OCI_BASE_URL, OCI_MODEL, and OCI_OPENAI_PROJECT in the shell where you run the script. |
401 or 403 | Confirm that you're using an OCI Generative AI API key and the correct regional OCI endpoint. Check the key's validity, project permissions, and access to the model or inference endpoint. |
404 for /responses/compact | Confirm that the base URL ends in /openai/v1. Check that the model ID or inference endpoint OCID is correct and available in the selected region. |
| A request reports missing project context. | Set OCI_OPENAI_PROJECT to the OCI Generative AI project OCID and pass it through project= when creating the SDK client. Project context is required for every model. |
| Automatic compaction doesn't return a compaction item. | Confirm that the request includes context_management with type: "compaction" and a threshold of at least 1000 tokens. Use a longer history or reduce a larger threshold while keeping it at or more than 1,000. |
The SDK has no responses.compact method. | Activate the virtual environment and install openai>=2.45.0. Check the installed version with python -m pip show openai. |
| A follow-up answer loses expected details. | For explicit compaction, pass the complete returned output array. For automatic compaction, keep the latest compaction item and all later output items. If no compaction item is returned, retain the existing history and new output. Compaction can lose details, so also evaluate the model and threshold with representative conversations. |
Next Steps
- Review Manage Context with Compaction for the API behavior and limits.
- Learn about OCI-managed conversation history.
- See the OpenAI compaction guide for the compatible SDK interface. Use the OCI-specific requirements in this tutorial for OCI requests.