LoopAI-103

Use the code_interpreter tool

Unit 3 of 8 · Module 4: Develop generative AI apps that use tools

Read this lesson

From thinker to doer

The code interpreter tool gives your model a Python runtime, where it can write and run code during a conversation. Instead of just talking about an algorithm, the model can test its logic, process data, and return real results. It turns the model from a thinker into a doer. It runs Python dynamically in a sandboxed environment. It handles files: you can upload, process, and download CSV, JSON, images, and more. It performs calculations, statistics, and data transformations on the fly. It sees the results of its code, so it can iterate and fix errors. And it tackles math problems, simulations, and logic puzzles through executable code. Common uses include data analysis, like summary statistics from a CSV; math and physics, like solving differential equations or running simulations; converting files between formats, like JSON and CSV; and prototyping algorithms before you implement them properly.

A simple example

To use it, add a code interpreter tool to the tools list, with a container of type auto, and mention the python tool in your instructions. Ask what the square root of sixteen is, and the output text says four. More importantly, the response details show the model worked it out by generating and running Python, using the math library. The process goes like this: you send a request that includes the tool. The model decides whether code is needed. It writes the Python. The code runs in a sandbox with common libraries, like pandas, numpy, and math. And the model reads the output, and folds it into its response.

Practices and limits

A few best practices. Be specific about the data format and the output you expect, and since many models call this the python tool, use that name in your instructions. Provide the domain context the task needs. Always review AI-generated code before you rely on it in production. Watch costs, because code execution adds tokens and resources. Lean on the pre-installed libraries, like pandas, numpy, and matplotlib. And let the model handle errors: it sees them and tries to fix them. Know the limits, too. The sandbox has no external network access. Some libraries may be missing, so tell the model if a standard one fails. Long runs hit timeouts. And memory is constrained, so very large datasets may need streaming or chunking.

Facts from Microsoft's documentation: Use the Azure OpenAI Responses API; Use Code Interpreter with Microsoft Foundry agents (CC BY 4.0), rewritten in Loop's own words and examples. Narration, animation, and quiz by Loop. The coding lab is based on code samples in Microsoft's Azure AI documentation (MicrosoftDocs/azure-ai-docs, MIT License; notice). Microsoft, Azure, and Microsoft Foundry are trademarks of Microsoft. Loop is independent and not affiliated with or endorsed by Microsoft.