Skip to main content

Fixed: Microsoft Agent Framework Agent Writes Code Instead of Running It with the Code Interpreter

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

I gave a Microsoft Agent Framework agent the hosted code interpreter and asked it to use code for a calculation. In every run it wrote C# into its answer and worked out the result itself; the code interpreter never ran. Requiring a tool call made it run Python but kept the C# in the answers. One sentence in the instructions, saying when to run code and not to write it, fixed both.

What I Was Doing​

I was building a small console app, CourseHelper, for the Fundamentals tutorial Tools the Model Service Runs. Its first agent had one tool, HostedCodeInterpreterTool, which lets the model service write code and run it in a sandbox. The agent helps students of a C# course:

Program.cs (the agent)
AIAgent agent = project.AsAIAgent(
model: "gpt-5.6-luna",
instructions: "You help students of a C# course. Answer in one or two sentences, in plain text.",
name: "CourseHelper",
tools: [new HostedCodeInterpreterTool()]);

AgentResponse response = await agent.RunAsync(
"Use code to count the days from 1 January 2026 to 25 December 2026.");

The app goes through the response and prints ran this code: with the code for every CodeInterpreterToolCallContent: the model service's report that it ran code.

What I Expected​

The service runs code that counts the days, the app prints that code, and the answer gives the result.

What I Got​

No code ran. The model wrote C# into its answer and gave the number itself:

Output
=== Code interpreter
> Use code to count the days from 1 January 2026 to 25 December 2026.
DateTime start = new DateTime(2026, 1, 1);
DateTime end = new DateTime(2026, 12, 25);
Console.WriteLine((end - start).Days); // 358 days (359 days if counting both dates inclusively)
(703 tokens in)

macOS-style window with two panes. Above, the instructions You help students of a C# course. Answer in one or two sentences, in plain text., the tool HostedCodeInterpreterTool and the question Use code to count the days from 1 January 2026 to 25 December 2026. Below, the output: the answer is C# code with DateTime and a comment saying 358 days, and no line saying the service ran code.

I asked the same question three more times. Each answer was a C# block with new DateTime(2026, 12, 25) - new DateTime(2026, 1, 1) and the comment // 358, and none of the four runs ran any code.

What I Checked​

Whether the tool works at all. With the same tool and question, and the instructions Answer in one or two sentences, in plain text., without the course, the service ran code in three runs out of three, and two of the answers began Using Python, the number of days from 1 January 2026 to 25 December 2026 is… The tool was there; the model chose not to call it.

What the code interpreter runs. Microsoft's Foundry documentation describes it creating a sandboxed Python environment. It runs Python, not C#.

Whether requiring a tool call fixes it. ChatOptions.ToolMode can demand a tool call. Its documentation: ChatToolMode.RequireAny indicating that tool usage is required, but that any tool can be selected. I passed it for the run:

Program.cs (one run that must call a tool)
AgentResponse response = await agent.RunAsync(question,
options: new ChatClientAgentRunOptions(new ChatOptions { ToolMode = ChatToolMode.RequireAny }));

The service then ran code in three runs out of three, but every answer still opened with a C# block, such as int days = (new DateTime(2026, 12, 25) - new DateTime(2026, 1, 1)).Days;. Forcing the call didn't change what the model thought the student wanted.

Why It Happens​

The model decides on its own whether to call a tool, from its instructions and the question. You help students of a C# course plus Use code reads like a request to show C# code, and showing code is something a course helper does in its answer. The code interpreter runs Python on the service, so for this agent, calling it looked like the wrong answer to the question. Nothing told the model that use code meant run code.

The Fix​

Say in the instructions when to run code, and not to write it:

Program.cs (the agent)
instructions: "You help students of a C# course. When a question needs a calculation, run Python code with your "
+ "code interpreter instead of writing code in your answer. Answer in one or two sentences of plain text, "
+ "without Markdown.",

After the Fix​

Output
=== Code interpreter
> Use code to count the days from 1 January 2026 to 25 December 2026.
ran this code:
import datetime
start = datetime.date(2026, 1, 1)
end = datetime.date(2026, 12, 25)
(end - start).days, (end - start).days + 1
There are 358 days between 1 January 2026 and 25 December 2026, or 359 days when counting both dates.
(880 tokens in)

macOS-style window with two panes. Above, the instructions with the new sentence When a question needs a calculation, run Python code with your code interpreter instead of writing code in your answer, the tool and the question. Below, the output: ran this code, four lines of Python with datetime.date, and the answer There are 358 days between 1 January 2026 and 25 December 2026, or 359 days when counting both dates.

With the new sentence, the service ran Python in every run I made: three runs of the question alone, and every full run of the app since, and none of the answers contained C# code.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming an offered tool gets usedThe model can answer without it, and nothing failsCheck the response for the tool's call content
Forcing a tool call with ChatToolMode.RequireAny aloneThe tool runs, but the answer still shows the code the model would have writtenSay in the instructions what the tool is for
Asking the code interpreter for C#It runs PythonAsk for the result, and let it write Python

Troubleshooting​

SymptomLikely causeFix
No CodeInterpreterToolCallContent in the responseThe model answered without the toolTell it in the instructions when to run code
The answer shows code next to a result the service computedThe instructions make code part of a good answerTell it not to write code in its answer

FAQ​

Why not always require a tool call?​

RequireAny forces a call to some tool in that run, so the model can no longer answer a question that needs no tool at all, and with several tools it still picks which one. In my runs it also left the C# in the answers. Instructions that say when to run code fix the cause.

Does running code cost extra?​

Yes. Microsoft's price list has a code interpreter session meter, $0.03 per session on the global meter when I checked, on top of the tokens.

Conclusion​

An agent framed as a C# course helper read use code as show C# code, so it never called the code interpreter, which runs Python. Requiring a tool call made it run code but kept the C#. One sentence in the instructions, When a question needs a calculation, run Python code with your code interpreter instead of writing code in your answer, made it run code and answer plainly in every run since.

Additional Resources​