← Blog

Claude API Coding Agent: Tool Use Guide

2026-11-14 · 5 min read · SubToAPI Team

If you're searching for "claude api coding agent tool use," you're almost certainly trying to build something that reads code, edits files, runs commands, and iterates — not just a chatbot that talks about code. The Claude API supports this through tool use: you define functions (read_file, write_file, run_tests, run_shell) as JSON schemas, Claude decides when to call them, your code executes them, and you feed the results back. This loop is the foundation of every coding agent, from simple autocomplete bots to multi-step refactoring tools.

The short answer: yes, Claude's tool use is well-suited for coding agents because it supports structured function calls, multi-turn tool result feedback, and (depending on model) parallel tool calls for independent operations like reading several files at once. The rest of this article covers how to actually wire that up — the tool schemas you need, the agent loop structure, and common failure modes.

What a coding agent actually needs

A minimal but useful coding agent needs four tool categories:

You don't need all of these on day one. A code review assistant might only need read + search. A full autonomous coding agent needs all four plus some kind of sandboxing.

Defining tools for a coding agent

Tool definitions are JSON schemas passed alongside your messages. Here's a realistic set for a basic agent:

[
  {
    "name": "read_file",
    "description": "Read the contents of a file by path",
    "input_schema": {
      "type": "object",
      "properties": {
        "path": { "type": "string" }
      },
      "required": ["path"]
    }
  },
  {
    "name": "write_file",
    "description": "Write content to a file, overwriting if it exists",
    "input_schema": {
      "type": "object",
      "properties": {
        "path": { "type": "string" },
        "content": { "type": "string" }
      },
      "required": ["path", "content"]
    }
  },
  {
    "name": "run_shell",
    "description": "Run a shell command in the project root and return stdout/stderr",
    "input_schema": {
      "type": "object",
      "properties": {
        "command": { "type": "string" }
      },
      "required": ["command"]
    }
  }
]

Keep descriptions concrete. "Read the contents of a file by path" works better than "Access file data" — vague descriptions lead to the model guessing wrong about when to call a tool.

The agent loop

The core pattern is always the same regardless of which API you're hitting:

  1. Send the user's task plus tool definitions
  2. If the response contains tool_use blocks, execute each one locally
  3. Send the results back as tool_result blocks in a new user message
  4. Repeat until the model returns a plain text response with no tool calls
async function runAgentLoop(client, messages, tools) {
  while (true) {
    const response = await client.messages.create({
      model: "claude-sonnet-4-5",
      max_tokens: 4096,
      messages,
      tools,
    });

    messages.push({ role: "assistant", content: response.content });

    const toolUses = response.content.filter(b => b.type === "tool_use");
    if (toolUses.length === 0) return response;

    const toolResults = [];
    for (const toolUse of toolUses) {
      const result = await executeTool(toolUse.name, toolUse.input);
      toolResults.push({
        type: "tool_result",
        tool_use_id: toolUse.id,
        content: JSON.stringify(result),
      });
    }

    messages.push({ role: "user", content: toolResults });
  }
}

This loop is identical whether you're hitting Claude directly or through an HTTPS wrapper — if you're building the agent as part of a product and need per-key usage tracking or a simpler auth model than managing provider credentials, that's exactly the gap SubToAPI fills: it turns your existing Claude access into a standard API with sub_live_ keys, so your agent's backend just does Authorization: Bearer $SUBTOAPI_KEY against https://api.subtoapi.app/v1/messages. The request/response shape for tool use stays the same, so the loop above doesn't change.

Execution safety

The part everyone underestimates is run_shell. Letting a model execute arbitrary shell commands is a real risk. Minimum safeguards:

For write_file, consider requiring the agent to produce a diff first and apply it only after a dry-run check (syntax validation, lint pass) succeeds. This catches a large fraction of bad edits before they touch disk.

Handling multi-file tasks

Coding tasks rarely touch one file. The model will often request several read_file calls in a single turn — this is the parallel tool call behavior, and your executeTool loop should run those concurrently where safe (reads) and sequentially where not (writes that depend on prior writes).

A pattern that works well: batch all read-only tool calls with Promise.all, then execute write/execute calls one at a time in the order returned.

Streaming for long agent runs

Coding agents can take many turns. If you're building a UI around this, stream each turn's text output so users see progress instead of staring at a spinner for 30+ seconds. SubToAPI's streaming endpoint follows the same SSE format as the underlying Claude API, documented at /docs/streaming, so you can pipe tokens to a terminal or web UI as they're generated without buffering full responses.

Keeping context under control

Every tool result goes back into the conversation. On a 10-turn agent run with large file reads, you'll hit context limits fast. Practical mitigations:

Getting started quickly

If you want to prototype a coding agent without first setting up provider billing and key management, start with the quickstart and the tool use docs — both use the same tools array and tool_result format shown above. Full request/response reference, including all content block types, is at /docs/messages. A free trial is available at signup, and plans scale from Solo at €9 to Team and Scale tiers for multi-seat setups — see pricing for details.

questions

Does Claude support parallel tool calls for coding agents? Yes. In a single turn, the model can request multiple tool calls — useful for reading several files before deciding on an edit. Your execution code should run independent calls concurrently and only serialize ones with dependencies.

What's the minimum tool set for a useful coding agent? read_file, write_file, and a search/grep tool cover most code review and small-edit use cases. Add run_shell only once you need test execution or build verification, and sandbox it.

How do I avoid blowing through context on long agent runs? Truncate large file reads to relevant sections, summarize completed tool results instead of keeping them verbatim, and split long multi-step tasks into separate conversations rather than one continuously growing thread.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →