Skip to content

LLM Orchestrator: How LLM Writes Code That Calls APIs

Summary

LLM can do more than just answer text. You can set it up as an architect: the model writes code that calls external APIs, and you run this code locally and control each step. Here is how this pattern works and how to apply it.

The Idea: Brain and Hands Separated

Usually, LLMs are seen as chatbots: ask a question, get an answer. But there is a second mode of operation, much more powerful:

flowchart LR
    M["LLM (brain)\ndecides what to do\nwrites code"] -->|"generates script"| C["Python script (hands)\ncalls API"]
    C -->|"HTTP request"| A["External service"]
    A -->|"response"| C
    C -->|"result"| M

The model does not magically "pull APIs". It generates code that you run. The model is the brain, deciding what to do; scripts are the hands, doing what was decided. This separation gives control: each step can be checked before it is executed.

This is how I worked in the DeepSeek architect case: the model designed auto-posting and migration, and the code called DeepSeek API for translation and Joomla Web Services for publishing.

Why This Works

LLMs are good at:

  • reading documentation and data structures;
  • writing neat scripts for specific tasks;
  • reasoning about sequences of steps.

But they are bad at:

  • pressing buttons in the real world;
  • guaranteeing no mistakes in routine tasks.

So the best combination is: the model makes decisions and writes code, and execution is handed over to deterministic code and a human.

Basic Template

The simplest option is to ask the model to write a function wrapper around an API:

import os
import requests

def call_api(endpoint: str, payload: dict) -> dict:
    resp = requests.post(
        os.environ["API_URL"] + endpoint,
        headers={"Authorization": f"Bearer {os.environ["API_KEY"]}"},
        json=payload,
        timeout=60,
    )
    resp.raise_for_status()
    return resp.json()

Secrets are not hardcoded into the code — they are in environment variables (.env). The model should know this from the prompt.

Model Calls Model Pattern

A special case is when the orchestrator calls another instance of LLM via API. For example, the architect model writes a translation script that calls the API of the same model:

def call_deepseek(system_prompt: str, user_text: str) -> str:
    resp = requests.post(
        os.environ["DEEPSEEK_API_URL"],
        headers={"Authorization": f"Bearer {os.environ["DEEPSEEK_API_KEY"]}"},
        json={
            "model": os.environ["DEEPSEEK_MODEL"],
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_text},
            ],
            "temperature": 0.2,
        },
        timeout=120,
    )
    resp.raise_for_status()
    return resp.json()["choices"][0]["message"]["content"]

It sounds like a "model calls model" — and that's exactly what it is. One instance designs and generates, the other translates or generates content via API.

How to Set the Task for the Model

To make the model work as an architect, not a chatbot, the prompt must fix the rules:

You are an engineer-architect. Work in read-only mode until I confirm changes.

1. First, study the data structure and draft a plan.
2. Show the plan (dry-run) and wait for my OK.
3. Only after OK, write scripts that execute the changes.
4. I run each script locally myself.
5. After each stage — a report with IDs of changed objects.
6. Do not perform mass changes outside of the agreed plan.

Key elements:

  • read-only until confirmation — the model reads first, writes later;
  • dry-run — a plan without actual changes;
  • human in the loop — each script is run manually;
  • reporting — specifics after each stage, not just "done".

Checkpoints

Where the model must stop and wait for a human:

  1. after studying the data — show what was found;
  2. after the plan — show what will be done (dry-run);
  3. before writing — get explicit "OK";
  4. after each writing stage — a report with the result.
flowchart TD
    A["Read-only: study"] --> B["Dry-run: plan without writing"]
    B --> C{"Human: OK?"}
    C -->|"no"| B
    C -->|"yes"| D["Writing: stage 1"]
    D --> E["Report"]
    E --> F{"All stages complete?"}
    F -->|"no"| C
    F -->|"yes"| G["Done"]

This is not "the model pretends to be cautious" — it is the consequence of rules embedded in the task setup. Agent autonomy is not "do everything yourself", but "do yourself, but stop where the cost of error is high".

What Not to Give the Model Without Control

  • Mass UPDATE/INSERT into the database. First one record — check, then the rest.
  • Data deletion. Only with explicit confirmation and backup.
  • Publishing to production. First draft, then publish.
  • Actions with paid resources. One run — calculate cost, then scale.

Conclusion

The LLM orchestrator pattern turns a chat into a full-fledged engineer: the model designs and writes code, code calls APIs, and you run and control each step. The key is not to let the model write directly into live systems, but to run everything through read-only, dry-run, and explicit confirmation.

See Also