Code Interpreter tool for Microsoft Foundry agents
Code Interpreter enables a Microsoft Foundry agent to run Python code in a sandboxed execution environment. Use this tool for data analysis, chart generation, and iterative problem-solving tasks that benefit from code execution. In this article, you create an agent that uses Code Interpreter, upload a CSV file for analysis, and download a generated chart. When enabled, 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, two Code Interpreter sessions are created. Each session is active by default for one hour with an idle timeout of 30 minutes.
Usage support
| Microsoft Foundry support | Python SDK | C# SDK | JavaScript SDK | Java SDK | REST API | Basic agent setup | Standard agent setup |
|---|---|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | - | - | - | ✔️ | ✔️ |
- indicates the feature isn’t currently available for that SDK or API.
Prerequisites
- Basic or standard agent environment. See agent environment setup for details.
- Latest prerelease SDK package installed (
azure-ai-projects>=2.0.0b1for Python). 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.You need the latest prerelease package. For more information, see the quickstart.
Sample of using agent with code interpreter tool in Python SDK
The following Python sample shows how to create an agent with the code interpreter tool, upload a CSV file for analysis, and request a bar chart based on the data. It demonstrates a complete workflow: upload a file, create an agent with Code Interpreter enabled, request data visualization, and download the generated chart. Set these environment variables:FOUNDRY_PROJECT_ENDPOINTFOUNDRY_MODEL_DEPLOYMENT_NAME
Expected output
The sample code produces output similar to the following example:Sample of using agent with code interpreter and file attachment in C# SDK
The following C# sample shows how to create an agent with the code interpreter tool and ask it to solve a mathematical equation. Replace the environment variable values (FOUNDRY_PROJECT_ENDPOINT, FOUNDRY_MODEL_DEPLOYMENT_NAME) with your actual resource details. The agent executes Python code in a sandboxed container to compute the solution. The code uses synchronous calls for simplicity. For asynchronous usage, refer to the code sample in the Azure SDK for .NET repository on GitHub.
Expected output
The sample code produces output similar to the following example: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.