Use case

Coding agents

Agents that read a repository, run tools and propose edits make many model calls per task. Most of the cost control is in which model handles which step, and in capping what one runaway loop can spend.

The problem

A single "fix this failing test" task can take 10 to 50 model calls: read files, search, run the tests, edit, run them again. Sending every call to the strongest model is slow and expensive; sending every call to a cheap model fails on the hard steps. And an agent stuck in a loop keeps spending until something stops it.

How it works with NeuronGate

  1. 01

    Task + tools

    Your agent sends messages and tool definitions

  2. 02

    Model plans

    Returns tool_calls instead of text

  3. 03

    Your code runs tools

    read_file, run_tests, apply_patch

  4. 04

    Results appended

    As role: tool messages

  5. 05

    Repeat or answer

    Until the model replies with text

One agent step: the model asks for a tool, your code runs it, the result goes back

Tool calling uses the standard OpenAI tools and tool_calls fields, so agent frameworks that speak the OpenAI format work unchanged. Because every model sits behind the same key, the agent can use one model to plan and another to execute without a second account or SDK.

Choosing models

Match the model to the step, not the task. Check current per-token prices and context windows in the model catalog; the roles below hold even as the specific models change.

Model roles in a coding agent
RoleGood fitWhyTrade-off
Planner / hard steps~anthropic/claude-sonnet-latestStrong at multi-file reasoning and reliable tool useHighest cost per call; keep it for decisions
Routine stepsdeepseek/deepseek-v4-flashCheap and fast for reading files, summarising output, small editsWeaker on long chains of reasoning
Second opinion~openai/gpt-latestA different model family catches different mistakes in reviewDoubles the cost of the steps you review
Huge context~google/gemini-flash-latestLarge context window for whole-repo or long-log questionsLong prompts cost more even on a cheap model

Example: a tool-calling loop

A minimal agent with one tool and a hard limit on steps. The limit matters: it is the first line of defence against a loop, before the key's spend cap.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://neurongate.net/v1",
    api_key=os.environ["NEURONGATE_API_KEY"],
)
import json
from pathlib import Path

tools = [{
    "type": "function",
    "function": {
        "name": "read_file",
        "description": "Read a file from the repository",
        "parameters": {
            "type": "object",
            "properties": {"path": {"type": "string"}},
            "required": ["path"],
        },
    },
}]

messages = [
    {"role": "system", "content": "You are a coding agent. Read files before answering."},
    {"role": "user", "content": "Why does tests/test_parse.py fail?"},
]

for step in range(10):  # hard cap on agent steps
    reply = client.chat.completions.create(
        model="~anthropic/claude-sonnet-latest",
        messages=messages,
        tools=tools,
    )
    msg = reply.choices[0].message
    messages.append(msg.model_dump(exclude_none=True))
    if not msg.tool_calls:
        print(msg.content)
        break
    for call in msg.tool_calls:
        path = json.loads(call.function.arguments)["path"]
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": Path(path).read_text()[:20_000],  # bound what goes back in
        })

Tool output is input tokens

Everything a tool returns is sent back on the next call and billed as input. Truncate file reads and test logs, and summarise long outputs with a cheap model before handing them to the planner.

Treat file contents as untrusted

A file or web page the agent reads can contain instructions aimed at the model. Allowlist the models the key may call and keep destructive tools (deleting files, pushing code) behind a confirmation in your code.

Cost control

  1. 1

    Give the workload its own key

    Create a key just for the agent under API keys. Its usage then shows up separately in the logs, and revoking it touches nothing else.
  2. 2

    Set a monthly spend cap

    Once the key has spent its cap for the month, its requests are refused until the next month or until you raise the cap. Size it from a week of real traffic, not a guess.
  3. 3

    Allowlist the models it may call

    Restrict the key to the models this workload actually uses, for example ~anthropic/claude-sonnet-latest and deepseek/deepseek-v4-flash. A typo or a prompt-injected model switch then fails instead of quietly running on a pricier model.
  4. 4

    Watch the logs

    Filter the request log by this key to see tokens, cost and latency per call. Export CSV for a monthly cost-per-workload report.

FAQ

Do agent frameworks work with NeuronGate?

Frameworks that call the OpenAI chat completions API with tools work by changing the base URL to https://neurongate.net/v1 and using a NeuronGate key. The integrations page has config for the common ones.

What happens if the model's provider has an outage mid-task?

If one host of the model fails, the request is retried on another host of the same model. NeuronGate never switches to a different model; if you want one, catch the error in your agent and retry with the model you choose.

How do I stop a looping agent from spending too much?

Cap the number of steps in your code, and give the agent its own key with a monthly spend cap. Once the cap is reached, that key's requests are refused while your other keys keep working.

Can the planner and worker models come from different providers?

Yes. Every model is called through the same endpoint and key, so mixing providers is just a different model string per call.