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.
Usage support
The following table shows SDK and setup support.| Microsoft Foundry support | Python SDK | C# SDK | JavaScript SDK | Java SDK | REST API | Basic agent setup | Standard agent setup |
|---|---|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
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.
- For file operations: CSV or other supported files to upload for analysis.
Code Interpreter isn’t available in all regions. See Check regional and model availability.
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.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
| File format | MIME type |
|---|---|
.c | text/x-c |
.cpp | text/x-c++ |
.csv | application/csv |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.html | text/html |
.java | text/x-java |
.json | application/json |
.md | text/markdown |
.pdf | application/pdf |
.php | text/x-php |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.py | text/x-python |
.py | text/x-script.python |
.rb | text/x-ruby |
.tex | text/x-tex |
.txt | text/plain |
.css | text/css |
.jpeg | image/jpeg |
.jpg | image/jpeg |
.js | text/javascript |
.gif | image/gif |
.png | image/png |
.tar | application/x-tar |
.ts | application/typescript |
.xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xml | application/xml or text/xml |
.zip | application/zip |
Troubleshooting
| Issue | Likely cause | Resolution |
|---|---|---|
| Code Interpreter doesn’t run. | Tool not enabled or model doesn’t support it in your region. | Confirm Code Interpreter is enabled on the agent. Verify your model deployment supports the tool in your region. See Check regional and model availability. |
| No file is generated. | Agent returned text-only response without file annotation. | Check response annotations for container_file_citation. If none exist, the agent didn’t generate a file. Rephrase the prompt to explicitly request file output. |
| File upload fails. | Unsupported file type or wrong purpose. | Confirm the file type is in the supported file types list. Upload with purpose="assistants". |
| Generated file is corrupt or empty. | Code execution error or incomplete processing. | Check the agent’s response for error messages. Verify the input data is valid. Try a simpler request first. |
| Session timeout or high latency. | Code Interpreter sessions have time limits. | Sessions have a 1-hour active timeout and 30-minute idle timeout. Reduce the complexity of operations or split into smaller tasks. |
| Unexpected billing charges. | Multiple concurrent sessions created. | Each conversation creates a separate session. Monitor session usage and consolidate operations where possible. |
| Python package not available. | Code Interpreter has a fixed set of packages. | Code Interpreter includes common data science packages. For custom packages, use Custom code interpreter. |
| File download fails. | Container ID or file ID incorrect. | Verify you’re using the correct container_id and file_id from the response annotations. |
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.
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.