Skip to main content
Code Interpreter enables a Microsoft Foundry agent to run Python code in a sandboxed execution environment. The agent’s Foundry model writes and executes code for data analysis, chart generation, and iterative problem-solving tasks.
Consider adding this tool using a toolbox. By using a toolbox, you are able to reuse the tool across agents and runtimes, as well as centralizing credential management, versioning, and policy enforcement through a managed MCP endpoint. See the toolbox quickstart.
In this article, you create an agent that uses Code Interpreter, upload a CSV file for analysis, and download a generated chart. When you enable Code Interpreter, your agent can write and run Python code iteratively to solve data analysis and math tasks, and to generate charts.
Code Interpreter has additional charges beyond the token-based fees for Azure OpenAI usage. If your agent calls Code Interpreter simultaneously in two different conversations, it creates two Code Interpreter sessions. Each session is active by default for one hour with an idle timeout of 30 minutes.

Prerequisites

  • Basic or standard agent environment. See agent environment setup for details.
  • Latest SDK package installed for your language. The .NET SDK is currently in preview. See the quickstart for installation steps.
  • Azure AI model deployment configured in your project.
Code Interpreter isn’t available in all regions. See Check regional and model availability.

Usage support

The following table shows SDK and setup support.

Create an agent with Code Interpreter

The following samples demonstrate how to create an agent with Code Interpreter enabled, upload a file for analysis, and download the generated output. Each file-upload sample generates a small CSV in the current working directory, uploads it, and then deletes the local temporary file.
You can customize Code Interpreter behavior at runtime, such as specifying which files to include or adjusting tool parameters per request, by using structured inputs.

Check regional and model availability

Tool availability varies by region and model. For the current list of supported regions and models for Code Interpreter, see Best practices for using tools in Microsoft Foundry Agent Service.

Supported file types

Troubleshooting

Clean up resources

Delete resources you created in this sample when you no longer need them to avoid ongoing costs:
  • Delete the agent version.
  • Delete the conversation.
  • Delete uploaded files.
For examples of conversation and file cleanup patterns, see Web search tool and File search tool for agents.

Sandboxed execution environment

Code Interpreter runs Python code in a Microsoft-managed sandbox. The sandbox is designed for running untrusted code and uses dynamic sessions (code interpreter sessions) in Azure Container Apps. Each session is isolated by a Hyper-V boundary. Key behaviors to plan for:
  • Region: The Code Interpreter sandbox runs in the same Azure region as your Foundry project.
  • Session lifetime: A Code Interpreter session is active for up to one hour, with an idle timeout (see the Important note at the beginning of this article).
  • Isolation: Each session runs in an isolated environment. If your agent invokes Code Interpreter concurrently in different conversations, separate sessions are created.
  • Network isolation and internet access: The sandbox doesn’t inherit your agent subnet configuration, and dynamic sessions can’t make outbound network requests.
  • Files in the sandbox: The sandboxed Python runtime has access to files you attach for analysis. Code Interpreter can also generate files, such as charts, and return them as downloadable outputs.
If you need more control over the sandbox runtime or you need a different isolation model, see Custom code interpreter tool for agents.