Cost of code interpreter (agents sdk)

The code interpreter tool is consuming a lot of money. Is the tool charging per token to execute the code? How does this charge work?

With the assistant API, my costs were very low, but now with agents, the costs have skyrocketed (+1000%).

The price is per container created.

How are you calling it? If you make an isolated call as a function call in a separated agent, it might be creating a new one on each tool call instead of reusing an existing one.

I’m letting the agent manage this itself, meaning it can sometimes use the code interpreter to do something and then respond to the user. The $0.03 per session isn’t the problem, but if you multiply that amount by the session total, the value doesn’t add up… Something is consuming a lot, and I believe it’s the cost per token.

The agents sdk work a little different than sequential responses API calls.

If you were using the responses API directly, it would use previous_response_id to recover the conversation and reuse the container.

In the agents sdk, if you have a custom tool, depending on how you are invoking the responses API in a separated thread it can’t reuse the container, calling it multiple times and creating new containers instead of reusing.

Have you checked the traces dashboard? That might give you some leads to figuring if this is the case on how it is using the tools.

You can see if it is the individual session ID teardown and getting a new one that is costing you right on the dashboard:

https://platform.openai.com/usage/code-interpreter-sessions

Then go after the particular model’s usage, which might be under “chat completions” - the underlying layer actually servicing Responses?

If you use an AI model that likes being “agentic”, and give it a 10k input, it might run that input 50 times doing stuff, while accumulating more chat history.

Persisting a conversation container ID of your own creation is also not a great solution - you might provide one to a chat session, keep pinging it for a while to avoid auto-expiry, but then you have a guaranteed bill for what the AI might never use.