Skip to main content
Article
ai-agentsllmautomationapi-integrationpython

Cloudflare Code Mode

Cloudflare Code Mode enables Large Language Models (LLMs) to generate and execute real code for tool calls, allowing them to interact with external APIs and services. This transforms LLMs into autonomous agents capable of performing automated tasks and complex workflows.

intermediate15 min5 steps
The play
  1. Receive & Interpret Prompt
    Provide the LLM with a natural language request that requires an external action or data retrieval. The LLM will analyze the prompt to understand the user's intent.
  2. Identify Necessary Tools
    Based on the prompt and its available tool definitions (e.g., functions, APIs), the LLM determines which specific external tool(s) can fulfill the request.
  3. Generate Executable Code
    The LLM dynamically generates actual, executable code (e.g., Python, JavaScript) that invokes the identified tool with the correct arguments extracted from the prompt. This is not pseudocode, but functional code.
  4. Execute Generated Code
    Run the generated code within a secure and isolated environment. This execution will trigger the external API call, database query, or other interaction defined by the tool.
  5. Integrate & Process Results
    Capture the output from the tool's execution and feed it back to the LLM. The LLM can then use this real-world information to continue the conversation, refine its understanding, or take further actions.
Starter code
import json

def get_weather(location: str, unit: str = "celsius"):
    """Fetches current weather for a given location."""
    if location.lower() == "london":
        return {"location": "London", "temperature": "15", "unit": unit, "conditions": "cloudy"}
    elif location.lower() == "new york":
        return {"location": "New York", "temperature": "22", "unit": unit, "conditions": "sunny"}
    else:
        return {"location": location, "temperature": "unknown", "unit": unit, "conditions": "unavailable"}

# Simulate LLM-generated executable code to call the 'get_weather' tool
# Prompt: "What's the weather in London?"

tool_name = "get_weather"
tool_args = {"location": "London", "unit": "celsius"}

# In a real scenario, the LLM would generate the call directly.
# Here, we simulate by calling the function with the LLM's 'decision'.
if tool_name == "get_weather":
    result = get_weather(**tool_args)
    print(json.dumps(result, indent=2))
else:
    print(f"Unknown tool: {tool_name}")
Cloudflare Code Mode — Action Pack