web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Copilot Studio / Copilot Studio custom ...
Copilot Studio
Suggested Answer

Copilot Studio custom prompt with Code Interpreter reuses previous code

(1) ShareShare
ReportReport
Posted on by 5

Hi everyone,

I’m building a Copilot Studio agent that answers business questions using large Excel files stored in SharePoint (structured data, huge volume). We enabled Code Interpreter because questions require filtering/aggregations and computation on the dataset.

What works (but is brittle)

We tried Microsoft’s approach for “Use code interpreter for analysis of a structured data file as a SharePoint knowledge source” (Use code interpreter to analyze structured data). It works, but it’s brittle: the agent needs to first “search knowledge” and then run Code Interpreter on every turn, otherwise the file isn’t available to the code step.

 

Alternative approach I tried (current problem)

Instead of relying on knowledge-first behavior, I created a Custom Prompt / Prompt action tool and pass:


  • Input A: Excel file (from SharePoint / file input)
  • Input B: User’s natural-language question

  •  

The tool has Code Interpreter enabled and is supposed to generate Python each time and compute results from the file.

Issue

The prompt tool does not reliably regenerate new Python/code per new question.
After answering the first question, when I ask a different question, it frequently:


  • reuses the same code, or
  • returns the same answer as the previous question, even though the user query changed
    This feels like stale context / caching / tool output reuse, where the prompt tool isn’t treating each invocation as a fresh analysis.

  •  

Repro steps


  1. Create a Prompt action / custom prompt tool and enable Code Interpreter. 
  2. Add file input parameter  + text input parameter (question).
  3. Add tool to an agent and call it from chat.
  4. Call the tool inside a topic or agent flow and use Sharepoint get file content connector to get the excel.
  5. Ask Q1 (e.g., “What is total sales for Product X in Jan?”) → correct.
  6. Ask Q2 (different question, e.g., “Top 5 territories by growth last 4 weeks?”) → tool often repeats Q1 logic/answer

  7.  

What I tried


  • Making the prompt more explicit: “Always write new Python code for the current question; ignore prior code.”
  • Adding strict instructions to recompute from the file each time.
    Still seeing repeated/previous answers

Ask / What I’m looking for


  1. Is there a known behavior where prompt tools reuse prior responses/code (context carryover) even when inputs change?
  2. Is there a supported way to force fresh execution per invocation, such as:
    • disabling conversational context for the tool,
    • forcing a “stateless” run,
    • a setting to prevent reuse/caching, or
    • recommended prompt pattern to guarantee regenerated Python and recomputation  
  3. For large SharePoint Excel data, what’s the most reliable pattern:
    • SharePoint knowledge source + Code Interpreter (knowledge-first), or
    • Prompt tool with file input + Code Interpreter, or
    • something else entirely for repeatable analytics?
  4.  Currently cannot use a db or dataverse so would like help for this scenario.

Any guidance/best practices would be really helpful—especially if someone has a stable pattern for “large structured Excel in SharePoint + deterministic recompute every user turn.”

Thanks!

Categories:
I have the same question (0)
  • Suggested answer
    Karan_Shewale_MSFT Profile Picture
    Microsoft Employee on at

    Code Interpreter in Copilot Studio runs inside the agent/prompt experience and follows normal multi‑turn behavior, so outputs can be influenced by prior turns unless you explicitly force reprocessing each time. There is no documented/official setting today to make Code Interpreter tool runs fully stateless per invocation (for example, “disable context” or “fresh session every call”).

    Most reliable pattern for large Excel in SharePoint: use the Microsoft‑documented SharePoint structured file knowledge source + Code Interpreter approach, which is designed for analyzing Excel/CSV from SharePoint in conversations. For maximum repeatability under heavy workloads (timeouts/limits), move computation to an agent flow and return results to the agent, since Code Interpreter has sandbox/runtime limitations (for example, session timeouts and restricted execution).

    References:

    Thanks 
    Karan Shewale.
  • NS-03031250-0 Profile Picture
    5 on at

    Thanks Karan, that helps clarify the expected behavior.

    Just to add more context from my side: in my case, I’m using a Custom Prompt that runs purely as a tool, not as part of a multi‑turn conversational memory. The tool is invoked with explicit inputs (Excel file + user question) and is expected to behave deterministically per invocation. However, even in this stateless-style setup, the Code Interpreter frequently returns the answer/code from the previously asked question, which strongly suggests residual context or tool execution reuse that isn’t obvious or controllable today.

    Additionally, I am also using the documented structured data + Code Interpreter approach
    (https://learn.microsoft.com/en-us/microsoft-copilot-studio/knowledge-code-interpreter-structured-data).

    After the recent Work IQ update, this approach is now returning too much data from the knowledge search step, which then causes OpenAI token limit errors before Code Interpreter can even run. This makes the “knowledge-first” pattern unreliable for large Excel files at scale.

    So right now, I’m hitting limitations on both paths:

    • Custom Prompt + Code Interpreter → stale answers / reused code across invocations
    • SharePoint knowledge source + Code Interpreter → excessive data grounding → token limit failures post–Work IQ

    Given the constraints (large structured Excel on SharePoint, no Dataverse/DB), I’m trying to identify:

    • whether there’s any supported way to truly force fresh Code Interpreter execution per tool call, and
    • what the most stable architecture is post–Work IQ for repeatable analytics use cases

    If anyone has found a workaround (for example, stricter file scoping, chunking patterns, or agent-flow-based execution that avoids these issues), I’d really appreciate hearing about it.

    Thanks again for the guidance.

  • Karan_Shewale_MSFT Profile Picture
    Microsoft Employee on at

    Thanks for the clear follow-up — your findings are valid.

    As of now, there is no documented setting in Copilot Studio to force Code Interpreter to run as a fully stateless/fresh session on every tool call. So even with Custom Prompt tool inputs, prior context influence can still happen.

    For the Work IQ token-limit issue, the safest pattern is to reduce data before Code Interpreter:

    • narrow file scope (split large Excel files),
    • pre-filter/chunk in an Agent Flow,
    • pass only the reduced dataset to Code Interpreter.

    Given your constraints (SharePoint files, no DB/Dataverse), the most reliable architecture is:

    1. Agent Flow for deterministic file retrieval/filtering,
    2. Code Interpreter only on scoped data,
    3. return final aggregates to chat.

    If possible, open a support ticket with conversation IDs/timestamps and a minimal repro, so Microsoft can confirm whether this is expected behavior or a tenant-specific issue.

  • DL-05051650-0 Profile Picture
    4 on at
    Hi, I have the same issue, except that my input is Dataverse. Has there been any update so far to prevent the code from being reused on every prompt call?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Copilot Studio

#1
Valantis Profile Picture

Valantis 639

#2
Vish WR Profile Picture

Vish WR 293

#3
Haque Profile Picture

Haque 216

Last 30 days Overall leaderboard