liveBook · Manning

Code execution

As explained in Reinforcement Learning from Human Feedback

Code execution is the process of running source code to obtain its computational behavior or result.

Code execution runs generated source code through a code interpreter and returns the result to a language model. As a special case of tool use, it lets models obtain precise answers to complex logic and mathematics problems rather than relying only on model weights. section 13.0 section 13.2

A typical execution loop interleaves generation with external computation:

  • The model generates tokens until it emits a code-execution request. section 13.2
  • An orchestrator runs the requested code and appends the output to the conversation. section 13.0 section 13.2
  • The model incorporates the returned value and continues generating, potentially making multiple tool calls. section 13.2

Computing Fibonacci numbers

For “What is the 50th Fibonacci number?”, the model generates a short Python loop, executes fib(50), receives 12586269025, and uses that output in its response. section 13.2

messages = [...]
while True:
    response = model(messages, tools=tools)
    if not response.tool_calls:
        return response.text

    for call in response.tool_calls:
        result = execute_tool(call.name, call.args)
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": result,
        })

This orchestration sketch shows how model messages, tool calls, execution results, and continued generation fit together. section 13.2

How Reinforcement Learning from Human Feedback covers it

How deep it goes
Defines execution as tool use, then traces calls, outputs, orchestration, and continued generation without detailing interpreter internals.
Examples
Demonstrates a Python Fibonacci loop that computes the 50th value and returns 12586269025.
What you can do after
Apply it via a code interpreter to compute exact results and feed those results back into model generation.
How it connects
Connects code execution to tool use, function calling, reasoning models, reinforcement learning from verifier feedback, and PPO.

Also explained in

Compare how 3 books explain code execution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Reinforcement Learning from Human Feedback. The section links point to the parts of the book it draws on.