[Agents]

Code Interpreter

Code Interpreter adds the capability to safely execute code in an isolated container, this built-in tool tool allows Agents to run code at any point on demand, practical to draw graphs, data analysis, mathematical operations, code validation, and much more.

code_interpreter_graph

To use the code interpreter, you can create an agent with the code interpreter tool, once done you can start a conversation with the agent and it will run code on demand, leveraging the outputs to answer your questions.

Create a Code Interpreter Agent

Create a Code Interpreter Agent

You can create an agent with access to our code interpreter by providing it as one of the tools.
Note that you can still add more tools to the agent, the model is free to run code or not on demand.

code_agent = client.beta.agents.create(
    model="mistral-medium-2505",
    name="Coding Agent",
    description="Agent used to execute code using the interpreter tool.",
    instructions="Use the code interpreter tool when you have to run code.",
    tools=[{"type": "code_interpreter"}],
    completion_args={
        "temperature": 0.3,
        "top_p": 0.95,
    }
)

As for other agents, when creating one you will receive an agent id corresponding to the created agent that you can use to start a conversation.

How it Works

How it Works

Now that we have our coding agent ready, we can at any point make use of it to run code.

Conversations with Code Interpreter

Conversations with Code Interpreter

To start a conversation with our code interpreter agent, we can use the following code:

response = client.beta.conversations.start(
    agent_id=code_agent.id,
    inputs="Run a fibonacci function for the first 20 values."
)
Explanation of the Output

Explanation of the Output

Below we will explain the different outputs of the response of the previous snippet example:

  • message.output: This entry corresponds to the initial response from the assistant, indicating that it can help generate the first 20 Fibonacci numbers.

  • tool.execution: This entry corresponds to the execution of the code interpreter tool. It includes metadata about the execution, such as:

    • name: The name of the tool, which in this case is code_interpreter.
    • object: The type of object, which is entry.
    • type: The type of entry, which is tool.execution.
    • created_at and completed_at: Timestamps indicating when the tool execution started and finished.
    • id: A unique identifier for the tool execution.
    • info: This section contains additional information specific to the tool execution. For the code_interpreter tool, the info section includes:
      • code: The actual code that was executed. In this example, it contains a Python function fibonacci(n) that generates the first n numbers in the Fibonacci sequence and a call to this function to get the first 20 Fibonacci numbers.
      • code_output: The output of the executed code, which is the list of the first 20 Fibonacci numbers.
  • message.output: This entry corresponds to the final response from the assistant, providing the first 20 values of the Fibonacci sequence.