#Agents accessing the annotations of other agents.as_tool
1 messages · Page 1 of 1 (latest)
Hi @unreal quest. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the available knowledge sources, when you use the OpenAI Agents SDK and model an agent as a tool via .as_tool, the orchestrator agent can call the sub-agent and receive its output as the tool's return value. However, the SDK documentation and examples focus on passing the final output (e.g., text, JSON, etc.) from the sub-agent to the orchestrator, and do not explicitly document a mechanism for passing through or exposing the full set of message annotations (such as file citations or file paths) generated by tools like Code Interpreter within the sub-agent's run.
In the OpenAI Assistants API, annotations (like file citations and file paths) are part of the message object and can be programmatically accessed after a run, allowing you to extract file references and download generated files. For example, you can retrieve a message, access its content and annotations, and process file links accordingly. This is well-documented for the API context, including how to replace annotation placeholders in text with download links or citations see here and here.
However, in the Agents SDK, when you use .as_tool, the orchestrator agent receives only the output as defined by the custom_output_extractor (if provided) or the last message from the sub-agent. There is no explicit documentation indicating that the orchestrator agent can directly access the full message object (including annotations) from the sub-agent's run. If you need to extract specific information (such as a JSON payload), you can implement a custom output extractor, but this is limited to what is present in the sub-agent's output, not the full message metadata or annotations see here.
Summary:
- In the Assistants API, you can access annotations (file citations, file paths) from message objects after a run.
- In the Agents SDK, when using
.as_tool, there is no documented way for the orchestrator agent to access the full set of annotations from the sub-agent; you only get the output as returned by the tool interface. - If you need annotation-like data, you would need to ensure the sub-agent includes this information in its output, or modify your workflow to extract and pass this data explicitly.
If you require direct access to annotations, you may need to use the Assistants API directly, or design your sub-agent to return the relevant file references in its output so the orchestrator can process them.
References:
- Assistants API: Message Annotations
- Agents SDK: Agents as Tools
- Code Interpreter: Reading Files Generated
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
https://openai.github.io/openai-agents-python/tools/
on this page it shows that if I need to set max _turns for an agent.as_tool, i can create a @function_tool that my master agent can use, that tool being the sub agent but with runner.run. If i use runner.run, how do i extract that agent's tool output for its code interpreter, for the annotations etc? @marble forge
https://openai.github.io/openai-agents-python/tools/
on this page it shows that if I need to set max _turns for an agent.as_tool, i can create a @function_tool that my master agent can use, that tool being the sub agent but with runner.run. If i use runner.run, how do i extract that agent's tool output for its code interpreter, for the annotations etc? @marble forge
Let me check the knowledge base for you again. One moment! :books:
If you use a custom @function_tool to run a sub-agent with Runner.run (for example, to set max_turns), the output you receive is a RunResult object. This object contains several useful properties:
- final_output: The final output of the agent (e.g., text, or an object if the agent has an output_type).
- new_items: A list of items generated during the agent run, which may include tool outputs.
- raw_responses: The raw model responses.
To extract tool outputs (such as those from the code interpreter) or similar annotation-like data, you should inspect the new_items property of the RunResult. The documentation suggests that you can scan new_items for specific output types, such as ToolCallOutputItem, to extract structured outputs (e.g., JSON, file references, etc.):
async def extract_json_payload(run_result: RunResult) -> str:
# Scan the agent’s outputs in reverse order until we find a JSON-like message from a tool call.
for item in reversed(run_result.new_items):
if isinstance(item, ToolCallOutputItem) and item.output.strip().startswith("{"):
return item.output.strip()
# Fallback to an empty JSON object if nothing was found
return "{}"
You can adapt this approach to look for outputs from the code interpreter or other tools. However, the documentation does not explicitly mention a standardized annotation structure (like file citations or file paths) in new_items. You will need to inspect the items and their types to extract the information you need. If the code interpreter outputs files or references, they should appear as items in new_items, and you can process them accordingly.
In summary:
- Use Runner.run to execute the sub-agent.
- Inspect run_result.new_items for tool outputs.
- Extract the relevant information (e.g., file references, JSON, etc.) by iterating over new_items and checking their type and content.
If you need more structured annotation data (like the Assistants API's message annotations), the knowledge sources do not provide explicit support for this in the Agents SDK. You may need to design your sub-agent to include such references in its output, or parse new_items for the information you need see example and guidance here.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
If i have 3 agents in my flow, 1 master agent and 2 sub agents..as_tools, and all three of them use their own code interpreter tools with containers: auto, does that create three separate containers? and i pay 3 times?
If i have 3 agents in my flow, 1 master agent and 2 sub agents..as_tools, and all three of them use their own code interpreter tools with containers: auto, does that create three separate containers? and i pay 3 times? @marble forge
Let me check the knowledge base for you again. One moment! :books:
Yes, if you have three agents (one master and two sub-agents), and each agent uses its own Code Interpreter tool with container: auto, this will result in the creation of three separate containers—one for each agent session. Each container is a fully sandboxed environment, and you are billed for each container created. The pricing is $0.03 per container creation see pricing.
The documentation states:
Code Interpreter is charged at $0.03 per container creation.
... you can do this by passing the"container": { "type": "auto" }property in the tool configuration while creating a new Response object. This automatically creates a new container, or reuses an active container that was used by a previouscode_interpreter_callitem in the model's context.
Containers
Since each agent is running independently and not sharing a container context, three separate containers will be created, and you will be billed for each one.
Summary:
- 3 agents using Code Interpreter with
container: auto= 3 containers created - You pay 3 × $0.03 = $0.09 for those sessions (plus any additional usage costs)
If you want to reuse a container across agents, you would need to explicitly create a container and pass its ID to each agent, but this is not the default behavior with container: auto see containers guide.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
the context object used to pass a container id across agents only exists for the run right? and can i pass the context to a sub agent that is used as a @function_tool with run.runner as shhown in the example?
the context object used to pass a container id across agents only exists for the run right? and can i pass the context to a sub agent that is used as a @function_tool with run.runner as shhown in the example? @marble forge
Let me check the knowledge base for you again. One moment! :books:
Yes, your understanding is correct:
-
The context object in the OpenAI Agents SDK is local to a single agent run. It is not persisted beyond the lifetime of that run. The context is designed to pass dependencies, data, or state (such as a container ID) to all tools, hooks, and sub-agents within that run. It is not sent to the LLM, but is available to your Python code (tools, hooks, etc.) see context management.
-
Passing context to a sub-agent used as a @function_tool:
Yes, you can pass the context object to a sub-agent that is invoked as a @function_tool. When you define a function tool that runs a sub-agent usingRunner.run, you can pass the same context object to the sub-agent’s run. This ensures that any data (such as a container ID) you store in the context is available to the sub-agent and its tools.
Here’s a simplified example from the documentation, showing how context is passed through a tool function to a sub-agent:
@function_tool
async def run_subagent(wrapper: RunContextWrapper[UserContext], input: str) -> str:
# Pass the same context to the sub-agent
result = await Runner.run(
starting_agent=sub_agent,
input=input,
context=wrapper.context, # Pass context here
)
return result.final_output
This pattern allows you to share data (like a container ID) between the master agent and sub-agents during a single run see context management.
Key points:
- The context object is only valid for the duration of the run.
- You can pass the context to sub-agents invoked as tools, ensuring shared access to data like container IDs.
If you need the context to persist across multiple runs, you would need to manage that persistence yourself outside the SDK.
References:
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge my chatbot is using flask as a backend. lets say that i save a containerid as a flask session variable, and that containerid is passed to every model through the context object.
first question is, say i pass the context object through every agent, if something is added to that context mid run, will it be available to the next agent who uses it? for example say my master agent invokes my data analysis agent .as_tool, and the data analysis agent uses code interpreter which creates a container. is it possible to add that container id to the context mid run, and when the data analysis is done doing its thing, will the master agent see the updated context object, all in the same run?
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources, the context object in the OpenAI Agents SDK is passed by reference within a single agent run. This means that if you modify the context object (for example, by adding a container ID) during the run—such as within a tool or sub-agent—those changes will be visible to other agents, tools, or hooks that access the same context object later in the same run.
From the documentation:
The context object is not sent to the LLM. It is purely a local object that you can read from, write to and call methods on it.
...every agent, tool function, lifecycle etc for a given agent run must use the same type of context.
You can use the context for things like:
- Contextual data for your run (e.g. things like a username/uid or other information about the user)
- Dependencies (e.g. logger objects, data fetchers, etc)
- Helper functions
The example provided in the documentation shows that the context object is mutable and shared across the run. Therefore, if your data analysis agent (invoked as a tool by the master agent) adds a container ID to the context, the master agent (and any subsequent tools or agents in the same run) will see the updated context object.
Summary:
- If you add or modify data in the context object mid-run (e.g., add a container ID), those changes will be visible to all agents and tools that use the same context object in that run.
- This allows you to share state (like a container ID) between agents and tools dynamically during the workflow.
Caveat:
This only applies within a single run. If you want to persist data across multiple runs (e.g., across different HTTP requests in Flask), you need to manage that persistence yourself (such as using Flask session variables), and re-inject the relevant data into the context object at the start of each run.
Reference:
Context management in Agents SDK
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge ok, i actually have a couple of agents in my flow and they all create/edit files using code interpreter. others also use sql and manually upload the files to openai and retrieve file-id (not in container but am thinking it can add to container as part of the flow). How can I manage the files that each of them make? say the sql agents each make files, and i want the data analysis agent to use those files, having access to the container which contains them. How would the data analysis agent know which file to use, especially if there are many other files accumulated in the container from past runs? and when the data analysis agent is done using code interpreter and outputs its own file, how do i know which file to return to the user via download link? i feel like adding a rule like the latest ""created_at" timestamp for fiunding which one to send to the user is too arbitrary and no good
Let me check the knowledge base for you again. One moment! :books:
Managing files across multiple agents and ensuring the correct files are used and returned in a multi-agent flow with Code Interpreter and containers is a nuanced challenge. Here’s what the knowledge sources say and what is possible:
1. File Management in Containers
- Files in Containers: When you use Code Interpreter with containers, files created or uploaded during a run are stored in the container’s file system. You can upload files to a container explicitly using the API, and files generated by the model (e.g., via Code Interpreter) are also stored there (Containers Guide).
- File Listing: The API allows you to list files in a container, but the knowledge sources do not provide detailed code or a recommended pattern for tracking which agent created which file within a container.
2. Tracking and Sharing Files Between Agents
- Explicit File Passing: The most reliable way to ensure the data analysis agent uses the correct files is to explicitly track and pass the file IDs or paths between agents via the context object. For example, after your SQL agent creates or uploads a file, it should record the file ID or path in the shared context. The data analysis agent can then read from the context to know exactly which files to use.
- Avoiding Ambiguity: Relying on metadata like the latest
created_attimestamp is, as you noted, arbitrary and error-prone, especially as containers accumulate files from multiple runs or agents.
3. Returning the Correct File to the User
- Annotations and Output: When Code Interpreter generates a file (e.g., a CSV or image), it cites the file in the message annotations. You can extract the file ID from these annotations in the message content after the run (Reading images and files generated by Code Interpreter).
- Programmatic Extraction: After the data analysis agent completes its run, inspect the output (and its annotations, if available) to programmatically determine which file was generated for the user. This is the recommended approach in the Assistants API, but the Agents SDK does not explicitly document annotation handling—so you may need to design your agent/tool output to include the file ID or path in its return value.
4. Best Practice from Knowledge Sources
- Explicitly Pass File References: Use the context object to pass file IDs/paths between agents and tools within a run. This avoids ambiguity and ensures each agent knows exactly which files to use.
- Design Output for Traceability: When an agent creates a file for user download, have it return the file ID/path explicitly in its output, so the master agent or your backend can generate a download link for the user.
Summary Table:
| Step | What to Do | How |
|---|---|---|
| SQL Agent creates file | Save file ID/path in context | context["sql_file_id"] = ... |
| Data Analysis Agent needs file | Read file ID/path from context | file_id = context["sql_file_id"] |
| Data Analysis Agent outputs file | Return file ID/path in output | e.g., {"result": ..., "output_file_id": ...} |
| User download | Use output file ID to generate link | Download via API |
Limitations:
- The knowledge sources do not describe a built-in, automatic way to tag files with agent provenance or to filter files by creator within a container.
- Annotation handling is well-documented for the Assistants API, but less so for the Agents SDK—so explicit context passing is the safest approach.
If you need more granular file management (e.g., tagging files with agent names or purposes), you will need to implement this logic in your agent/tool outputs and context handling.
References:
- Work with files in containers
- Reading files generated by Code Interpreter
- Context management in Agents SDK
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
are file names available in the containers file storage? if i give the file name and id to an agent whose code interpreter uses that container, will it be able to pick it amongst the others with accuracy? ALSO another question is whether TOOLS can be passed as context. For example, can I pass a context object to all my agents and that object is essentially the code interpreter tool initialized with a specific container-id @marble forge
Let me check the knowledge base for you again. One moment! :books:
1. Are file names available in the container’s file storage?
Yes, file names are available in the container’s file storage. When you list or retrieve files from a container, the file object includes both the file ID and the file path (which contains the file name). For example, when retrieving a container file, the response includes a path field such as:
{
"id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
"object": "container.file",
"created_at": 1747848842,
"bytes": 880,
"container_id": "cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04",
"path": "/mnt/data/88e12fa445d32636f190a0b33daed6cb-tsconfig.json",
"source": "user"
}
The path includes the file name, so if you provide both the file name and file ID to an agent whose code interpreter uses that container, it can accurately identify and use the correct file among others in the container (Retrieve container file, Work with files).
2. Can tools be passed as context?
The knowledge sources do not mention the ability to pass initialized tool objects (such as a code interpreter tool instance) via the context object. The context is intended for passing data, dependencies, or helper functions (such as file IDs, container IDs, or utility functions), but there is no documentation indicating that you can pass an already-initialized tool instance (like a code interpreter tool bound to a specific container) through the context to agents (Context management).
Instead, the recommended approach is to pass identifiers (like a container ID) via the context, and then configure the tool within each agent to use that container ID as needed.
Summary:
- File names are available in container file storage and can be used (with file IDs) to accurately select files.
- Tools themselves cannot be passed via context according to the knowledge sources; pass configuration data (like container IDs) instead.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
if im not mistaken, all the agents in the system are initialized when i use runner.run on the master agent (the master agent imports all of them). So how am i supposed to have them use a particular tool config (like code interpreter with container: "containerid") if that container is created within that same run? if its already created from a past run then sure, it will persist, but its gotta be created on demand once needed because it costs money, can t create a container every time
if im not mistaken, all the agents in the system are initialized when i use runner.run on the master agent (the master agent imports all of them). So how am i supposed to have them use a particular tool config (like code interpreter with container: "containerid") if that container is created within that same run? if its already created from a past run then sure, it will persist, but its gotta be created on demand once needed because it costs money, can t create a container every time @marble forge
Let me check the knowledge base for you again. One moment! :books:
You are correct in your understanding: if you want to avoid unnecessary container creation (and associated costs), you should create the container only when it is actually needed during a run, not at agent initialization.
How to handle this, based on the knowledge sources:
- Agents are typically initialized at the start of your application, but the tool configuration (such as the container ID for Code Interpreter) can be set or updated at run time.
- The Agents SDK allows you to pass a context object to each run. This context can include a container ID, which you can create on demand (e.g., via the OpenAI API) at the point in your flow when you know you need it.
- When you reach the point in your flow where a container is required (for example, when a sub-agent needs to use Code Interpreter), you can:
- Check if a container ID is already present in the context.
- If not, create a new container using the API (see container creation), and store the new container ID in the context.
- Configure the tool (e.g., Code Interpreter) for the agent at that moment, using the container ID from the context.
The SDK and API do not require you to bind the tool configuration (like container ID) at agent initialization—you can set or update it at run time, just before invoking the agent or tool. This allows you to create containers only when needed and share the container ID via the context object for the rest of the run.
Key points from the knowledge sources:
- The context object is mutable and shared within a run, so you can add the container ID to it after creating the container, and all subsequent agents/tools in that run can access it (context management).
- You can create containers on demand using the API, and then pass the container ID to the tool configuration as needed (containers guide).
In summary:
- Initialize agents at app start, but configure tools (like Code Interpreter with a specific container) at run time, after creating the container and storing its ID in the context.
- This approach ensures you only create containers when needed, and all agents in the run can use the same container by reading the ID from the context.
If you need to update an agent's tool configuration mid-run, you may need to re-instantiate the agent or update its tool list with the new configuration, as the SDK does not document dynamic reconfiguration of tools on an already-running agent instance. The knowledge sources do not provide explicit code for this, so you may need to experiment with agent instantiation timing and tool assignment.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
HOW do i configure a tool at runtime? @marble forge
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not provide explicit, step-by-step documentation or code examples for configuring (or reconfiguring) a tool at runtime in the OpenAI Agents SDK. The available documentation describes how to configure tools when initializing an agent, and how to pass context objects at runtime, but does not detail dynamic tool reconfiguration after agent instantiation.
What is documented:
- When you create an agent, you specify its tools (including tool configuration such as the container for Code Interpreter) as part of the agent’s initialization (Agents documentation).
- The context object can be used to pass data (like a container ID) at runtime, and this data can be accessed by tools or agents during the run (Context management).
- The CodeInterpreterTool takes a
tool_configparameter, which includes the container and other settings (CodeInterpreterTool reference).
What is not documented:
- There is no example or explicit mention of changing a tool’s configuration (such as swapping the container ID) after the agent has been created.
- There is no mention of a built-in method to dynamically update an agent’s tool list or tool configuration mid-run.
What you can do, based on the documentation:
- You can create a new agent instance at runtime with the desired tool configuration, using the container ID you have just created or retrieved. This is the documented way to ensure the tool uses the correct configuration.
- You can pass the container ID via the context, and design your tool or agent logic to read from the context and use the appropriate container.
Summary:
- The knowledge sources do not describe a way to reconfigure a tool on an existing agent instance at runtime.
- The recommended approach, based on the documentation, is to create a new agent instance with the desired tool configuration when you need to use a new container or other dynamic setting.
If you need more dynamic behavior, you may need to implement your own logic to instantiate agents with the correct configuration as needed.
References:
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge can you show me exactly how the code interpreter tool is configured for an agent in the agents sdk
Let me check the knowledge base for you again. One moment! :books:
Certainly! In the OpenAI Agents SDK, the Code Interpreter tool is configured for an agent by including it in the agent’s tools list during initialization. The tool is represented by the CodeInterpreterTool class, which takes a tool_config parameter specifying the container and other settings.
Here’s a direct example from the knowledge sources:
from agents import Agent, CodeInterpreterTool
agent = Agent(
name="Assistant",
tools=[
CodeInterpreterTool(
tool_config=CodeInterpreter(
container="cntr_abc123" # The container ID or config
)
)
],
)
- The
CodeInterpreterToolis initialized with atool_configthat includes the container information. - The container can be a specific container ID or a configuration object, depending on your use case.
This is how you configure the Code Interpreter tool for an agent in the Agents SDK as shown in the official documentation.
Note: The knowledge sources do not provide an example of changing this configuration at runtime; the tool is configured at agent initialization.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge the documentation only shows tool_config, but how do you know what the config setitngs are
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources indicate that the CodeInterpreterTool in the Agents SDK is configured using the tool_config parameter, which should be an instance of CodeInterpreter. However, the sources do not provide a detailed schema or list of all possible configuration settings for the CodeInterpreter object. The only explicitly documented setting is the container, which can be set to a container ID or configuration object:
CodeInterpreterTool(
tool_config=CodeInterpreter(
container="cntr_abc123" # Example container ID
)
)
If you need to know all available configuration options for CodeInterpreter, the knowledge sources do not specify them. You may need to consult the SDK source code or additional documentation not included in the provided sources for a complete list of settings see reference.
Summary:
- The only documented config setting is
container. - The full set of config options for
CodeInterpreteris not detailed in the knowledge sources.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge searcj the web for specifics on configuring the tool for agents sdk (not responses api)
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources do not provide detailed information or a comprehensive schema for configuring the CodeInterpreterTool in the OpenAI Agents SDK beyond specifying the container parameter. The only explicitly documented configuration is passing a container ID or configuration object to the tool_config parameter when initializing the tool for an agent as shown here.
If you need more specifics on additional configuration options, the knowledge sources do not contain enough information to answer your question.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge do containers ever expire if i continuously use them?
Let me check the knowledge base for you again. One moment! :books:
If you continuously use a container, it will not expire. According to the documentation:
A container expires if it is not used for 20 minutes. When this happens, using the container in
v1/responseswill fail. ... Any container operation, like retrieving the container, or adding or deleting files from the container, will automatically refresh the container'slast_active_attime.
So, as long as you keep performing operations on the container (such as running code, retrieving, or modifying files), the expiration timer is refreshed and the container remains active see Containers – Expiration.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
lets say i create a container BEFORE a run, and i want to pass it to every agents' code interpreter tool config. how would i do that? how do you actually use the context object for that specific dependency? @marble forge
Let me check the knowledge base for you again. One moment! :books:
To use a pre-created container with every agent’s Code Interpreter tool in the OpenAI Agents SDK, you should:
- Create the container before the run (using the API).
- Pass the container ID in the context object when you start the run.
- Configure each agent’s Code Interpreter tool to use the container ID from the context.
However, the knowledge sources do not provide a direct example of dynamically reading the container ID from the context inside the tool configuration at agent initialization. The documented approach is to set the container ID directly in the tool configuration when you create the agent (CodeInterpreterTool reference).
What is documented:
- The context object is passed to all agents, tools, and hooks during a run, and can carry dependencies like a container ID (Context management).
- The Code Interpreter tool is configured at agent initialization with a
containerparameter.
Typical usage (from documentation):
from agents import Agent, CodeInterpreterTool
agent = Agent(
name="Assistant",
tools=[
CodeInterpreterTool(
tool_config=CodeInterpreter(
container="cntr_abc123" # Pass your pre-created container ID here
)
)
],
)
You would repeat this for each agent, using the same container ID.
How to use the context object:
- You can store the container ID in the context object and, before initializing your agents, read the container ID from the context and set it in each agent’s tool configuration.
- The knowledge sources do not show a pattern where the tool configuration dynamically reads from the context at runtime; it must be set at agent creation.
Summary:
- Create the container before the run.
- Store the container ID in your context object.
- When initializing each agent, set the Code Interpreter tool’s
containerparameter to the container ID from your context. - There is no documented way to have the tool config dynamically read from the context during the run; you must set it up front.
References:
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge how can i extract the annotations file and container ids from an agent runner.run result
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not provide a direct example or explicit documentation for extracting annotation file and container IDs from an agent Runner.run result in the OpenAI Agents SDK.
What is documented is that the result of Runner.run is a RunResult object, which contains properties such as final_output, new_items, and raw_responses. The new_items list may include tool outputs, but the structure and presence of annotation data (such as file or container IDs) are not detailed for the Agents SDK. The documentation does show how to extract structured outputs (like JSON) from new_items by iterating over them and checking their type, but it does not specify how to extract annotation metadata similar to the Assistants API's message annotations (Agents SDK Results).
Summary:
- The knowledge sources do not explicitly describe how to extract annotation file and container IDs from a
Runner.runresult in the Agents SDK. - You may need to inspect the
new_itemsproperty of theRunResultfor tool outputs, but the exact structure for annotations is not documented.
If you require annotation extraction similar to the Assistants API, you may need to design your agent/tool outputs to include this information explicitly, as the SDK does not provide built-in support for this.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge does the code interpreter actually edit files in the container or does it duplicate them and edit the duplicate? in other words if i input a file asking it to add a column to it, will there then be 2 files in the container or just one
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not explicitly state whether the Code Interpreter edits files in place or creates a new file when making modifications (such as adding a column). However, the documentation and examples consistently show that when the Code Interpreter performs an operation that modifies a file (e.g., shuffling rows in a CSV or generating a new image), it creates a new file and cites this new file in the output annotations. The original file remains unchanged in the container.
For example, when a file is modified, the output message includes a reference to a new file (with a new file ID and path), and the user is provided a link to download this new file, not the original one. This suggests that the Code Interpreter duplicates the file and applies changes to the duplicate, rather than editing the original file in place see example.
Summary:
If you ask the Code Interpreter to add a column to a file, you should expect to see two files in the container: the original and the newly modified version.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge can i add a code interpreter tool with specific config to context object and it will be passed to every agent?
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources, you cannot add a fully initialized Code Interpreter tool (with specific config) to the context object and have it automatically used by every agent. The context object in the OpenAI Agents SDK is designed to pass data, dependencies, and helper functions (such as container IDs, file IDs, or utility functions), but not tool instances themselves. There is no documentation or example showing that you can pass a tool object via context and have agents automatically use it as their tool.
The recommended approach is to pass configuration data (like a container ID) via the context object, and then configure each agent’s Code Interpreter tool to use that data when you initialize the agent. The tool itself must be set in the agent’s tools list at initialization, not dynamically injected via context during a run see context management.
Summary:
- You can pass configuration data (e.g., container ID) via context.
- You cannot pass a tool instance via context for agents to use as their tool.
- Each agent’s tools must be configured at initialization, not via context.
If you need to share a specific configuration, pass the config data (not the tool) in the context and use it when initializing each agent’s tool.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
can you show me how that would be done? say my context object is a simple dictionnary, first field being container_id. Then i have one python file defining each agent in my agentic system. The ones that have code interpreter need the container id in their tool configs. how would i extract it from the context object
can you show me how that would be done? say my context object is a simple dictionnary, first field being container_id. Then i have one python file defining each agent in my agentic system. The ones that have code interpreter need the container id in their tool configs. how would i extract it from the context object @marble forge
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not provide a direct code example for dynamically extracting a container_id from a context object (such as a dictionary) and injecting it into the Code Interpreter tool configuration at agent initialization. The documented pattern is to set the tool configuration (including the container parameter) when you create the agent, not dynamically during a run see CodeInterpreterTool reference.
What is documented:
- You can pass configuration data (like
container_id) in the context object to all agents during a run see context management. - The Code Interpreter tool is configured at agent initialization, and the
containerparameter must be set at that time.
How you would do it (based on the documentation):
- Create your context object (e.g., a dictionary) with the
container_id. - When initializing each agent, read the
container_idfrom the context and set it in the tool config.
Example (based on documented patterns):
# Suppose this is your context object
context = {"container_id": "cntr_abc123"}
# In your agent definition file
from agents import Agent, CodeInterpreterTool, CodeInterpreter
def create_agent_with_code_interpreter(context):
container_id = context["container_id"]
agent = Agent(
name="Data Analysis Agent",
tools=[
CodeInterpreterTool(
tool_config=CodeInterpreter(
container=container_id # Set from context at initialization
)
)
],
)
return agent
Usage
agent = create_agent_with_code_interpreter(context)
- You **extract the `container_id` from the context** before agent creation and use it to configure the tool.
- This must be done **before** the agent is instantiated, as the SDK does not support dynamic reconfiguration of tools after initialization.
**Summary:**
- Extract the `container_id` from your context object before creating each agent.
- Pass it to the Code Interpreter tool config at agent initialization.
- There is no documented way to inject the container ID from context into an already-initialized agent or tool.
If you need to change the container during a run, you would need to create a new agent instance with the updated tool configuration.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
how do you set up code interpreter for an agent in its tools? @marble forge
Let me check the knowledge base for you again. One moment! :books:
To set up the Code Interpreter for an agent in its tools, you include the CodeInterpreterTool in the agent’s tools list during initialization. You configure it by passing a tool_config parameter, which specifies the container (either by ID or configuration object). Here’s how it’s shown in the knowledge sources:
from agents import Agent, CodeInterpreterTool
agent = Agent(
name="Assistant",
tools=[
CodeInterpreterTool(
tool_config=CodeInterpreter(
container="cntr_abc123" # The container ID or config
)
)
],
)
- The
CodeInterpreterToolis initialized with atool_configthat includes the container information. - The container can be a specific container ID or a configuration object, depending on your use case.
This approach is documented in the Agents SDK reference.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
are you sure thats the exact way to do it ? @marble forge
Let me check the knowledge base for you again. One moment! :books:
Thank you for your follow-up. Based strictly on the knowledge sources provided, the documented way to set up the Code Interpreter tool for an agent in the OpenAI Agents SDK is to include the CodeInterpreterTool in the agent’s tools list and provide a tool_config parameter with a container setting. The example from the official documentation is:
from agents import Agent, CodeInterpreterTool
agent = Agent(
name="Assistant",
tools=[
CodeInterpreterTool(
tool_config=CodeInterpreter(
container="cntr_abc123" # The container ID or config
)
)
],
)
This is the only explicit pattern shown in the knowledge sources for configuring the Code Interpreter tool for an agent. The documentation does not provide further details on additional configuration options or alternative setup methods for the tool in the Agents SDK see CodeInterpreterTool reference.
If you need more advanced or dynamic configuration, the knowledge sources do not provide enough information to confirm how that would be done.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Hi all,
I using the agents python sdk for an agent with the built in CodeInterpreterTool. This tool is being used and is returning a file inside the container. I am trying to extract the file-id from its tool output but am having a hard time figuring out how exactly. I cant simply ask the AI to give me the file id because I dont think it can see that and will make something up. I know its embeded somewhere in the RunResult object, but am really at a loss for how exactly to extract it. @marble forge
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources, the OpenAI Agents SDK’s Runner.run returns a RunResult object, which contains several properties such as final_output, new_items, and raw_responses. However, the documentation does not provide a direct or explicit example of extracting file IDs (such as those generated by the Code Interpreter tool) from the tool output in the RunResult object.
The most relevant information is that you can inspect the new_items property of the RunResult, which contains the items generated during the agent run, including tool outputs. The documentation shows how to extract structured outputs (like JSON) from new_items by iterating over them and checking their type, but it does not specify the exact structure for file outputs or file IDs from the Code Interpreter tool.
Here’s the closest documented pattern for extracting structured tool outputs:
async def extract_json_payload(run_result: RunResult) -> str:
for item in reversed(run_result.new_items):
if isinstance(item, ToolCallOutputItem) and item.output.strip().startswith("{"):
return item.output.strip()
return "{}"
You would need to adapt this approach to look for file references, but the knowledge sources do not provide the exact class or structure for file outputs.
Summary:
- The knowledge sources do not provide a direct method or code example for extracting file IDs from the Code Interpreter tool output in the Agents SDK.
- You may need to inspect
run_result.new_itemsfor tool outputs, but the exact structure for file outputs is not documented. - If you need to reliably extract file IDs, you may need to design your agent/tool output to include this information explicitly, as the SDK does not provide built-in support for this.
Reference:
Agents SDK Results
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge can we reverse engineer this by writing a logger.info that will show the full full RunResult, containing every single embedded object/property, without explicitly naming them? how would you write this?
Let me check the knowledge base for you again. One moment! :books:
Yes, you can log the full RunResult object to inspect all its embedded properties and objects. According to the knowledge sources, the RunResult class implements a __str__ method that uses pretty_print_result(self), which should provide a readable string representation of the entire object and its contents (RunResult reference).
To log the full RunResult, you can simply use:
import logging
# Assuming `result` is your RunResult object
logging.info(result)
Or, if you want to ensure you get the string representation:
logging.info(str(result))
This will output the full structure of the RunResult, including all embedded objects and properties, without needing to explicitly name them. This approach is supported by the SDK’s design, as the __str__ method is intended for pretty-printing the result object (RunResult reference).
If you want even more detail (such as the raw Python object structure), you could also use the vars() function or the pprint module, but the knowledge sources specifically mention the __str__ method for this purpose.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
when i print just the result, it doesnt actually show the whole thing. it seems to be cut off and it says "See RunResult..."
when i print just the result, it doesnt actually show the whole thing. it seems to be cut off and it says "See RunResult..." @marble forge
Let me check the knowledge base for you again. One moment! :books:
You're correct—when you print the RunResult object directly, the output uses the pretty_print_result function, which may summarize or truncate the output and display a message like "See RunResult..." instead of showing the full, deeply nested structure. This is by design, as noted in the SDK's documentation for the __str__ method of RunResult RunResult reference.
The knowledge sources do not provide a method to force the SDK to print the entire, fully expanded object with all nested properties. If you need to inspect every embedded property, you may need to manually traverse the attributes (such as new_items, raw_responses, etc.) and log them individually. The SDK does not document a built-in way to override or expand the pretty-printing behavior to show everything at once.
Summary:
- Printing the
RunResultobject directly may not show the full structure; it can be truncated by the pretty printer. - The knowledge sources do not provide a way to force full, untruncated output of all embedded properties in one go.
- For deeper inspection, you will need to access and log the relevant properties (like
new_items) individually.
If you need to see the full details, consider iterating over and logging the specific attributes you are interested in.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge so are you sure that there is no way to get the annotations container id and file id from the agents sdk when it uses code interpreter tool? i know that for responses api call its very easy and its in json
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources, there is no explicit documentation or example showing how to extract annotation data—such as container IDs and file IDs—from the output of the Code Interpreter tool when using the Agents SDK. The documentation for the Agents SDK describes how to access properties like new_items in the RunResult object, but it does not specify that annotation objects (as found in the Responses API) are present or accessible in the same way. The structure and extraction of file or container IDs from tool outputs in the Agents SDK are not detailed in the provided sources see Agents SDK Results.
In contrast, as you mentioned, the Responses API and Assistants API provide clear, structured JSON output with annotations for file and container IDs, making extraction straightforward.
Summary:
- There is no documented way in the knowledge sources to directly extract annotation container IDs and file IDs from the Agents SDK when using the Code Interpreter tool.
- The Responses API does provide this information in a structured, accessible format, but the same is not documented for the Agents SDK.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge i see that it returned this:
ci_68552d76d4b4819bb79bebd3b66c5bca0fc94151bbc49aca
is this a code interpreter tool call id? is there an api or something that i can use to manually inspect that and find the resulting files and all other information about that code interpereter tool call?
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources, the string you provided (e.g., ci_68552d76d4b4819bb79bebd3b66c5bca0fc94151bbc49aca) appears to be a Code Interpreter tool call ID. In the OpenAI API and Agents SDK, tool calls—including those for the Code Interpreter—are assigned unique IDs.
However, the knowledge sources do not document an API endpoint or method that allows you to manually inspect a Code Interpreter tool call by its ID and retrieve associated files or detailed information about that specific call. While the Responses API and Assistants API provide structured outputs (including file IDs and annotations) in their responses, there is no mention of a public API for querying tool call details by ID in the Agents SDK or the broader OpenAI API documentation provided.
Summary:
- The ID you see is likely a Code Interpreter tool call ID.
- There is no documented API or method in the knowledge sources to manually inspect a tool call by its ID and retrieve its resulting files or other details.
- If you need to access files or outputs, you must extract them from the run result or output objects as they are returned by the API or SDK, not by querying with the tool call ID afterward.
If you need more granular inspection, you may need to design your workflow to capture and store the relevant output information at the time of the run, as retroactive inspection by tool call ID is not documented in the provided sources.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
is there any documentation that says what we can use the code interpereter tool call id for? @marble forge
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources do not contain any documentation that explains what the Code Interpreter tool call ID can be used for, or how it can be used in further API calls or inspection. There is no mention of any specific functionality, endpoint, or use case for the Code Interpreter tool call ID in the provided materials.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge what about the container id, is there a way to inspect whats been going on in there? specifically what the most recent files added were?
Let me check the knowledge base for you again. One moment! :books:
Yes, you can inspect a container and see the files it contains, including information about the most recently added files. The OpenAI API provides endpoints to list all files in a container and retrieve metadata for each file, such as creation time (created_at), file ID, file path, and source.
To list the files in a container, use:
curl <https://api.openai.com/v1/containers/{container_id}/files> \
-H "Authorization: Bearer $OPENAI_API_KEY"
The response will include a list of file objects, each with metadata including the created_at timestamp. You can sort these to determine which files were added most recently. Here’s an example response:
{
"object": "list",
"data": [
{
"id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
"object": "container.file",
"created_at": 1747848842,
"bytes": 880,
"container_id": "cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04",
"path": "/mnt/data/88e12fa445d32636f190a0b33daed6cb-tsconfig.json",
"source": "user"
}
],
"first_id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
"has_more": false,
"last_id": "cfile_682e0e8a43c88191a7978f477a09bdf5"
}
You can use the created_at field to identify the most recent files. There is no endpoint to see a full activity log or history of operations inside the container, but you can always list and inspect the files currently present see List container files and Container file object.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@unreal quest Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@marble forge are there any endpoints for fetching files by openai path?
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not mention any OpenAI API endpoints that allow you to fetch files directly by their OpenAI path (such as /mnt/data/filename.csv). All documented endpoints for retrieving files require a file ID (e.g., cfile_...) and, for container files, also the container ID. There is no documentation of an endpoint that accepts a file path as a parameter for file retrieval see Retrieve container file content and Retrieve file content.