> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kymaapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a Coding Agent

> Agent that writes and executes code using tool calling.

## Best Model for This

| Model          | Why                                   | Cost per agent step |
| -------------- | ------------------------------------- | ------------------- |
| `qwen-3-coder` | Purpose-built for code, best accuracy | \~\$0.03            |
| `kimi-k2.6`    | Best tool calling, 262K context       | \~\$0.05            |
| `minimax-m2.5` | SWE-bench 80.2%, top agentic coding   | \~\$0.04            |

Costs assume \~1K tokens input + \~500 tokens output per step.

## Quick Start

<CodeGroup>
  ```python Python theme={null}
  import json
  from openai import OpenAI

  client = OpenAI(
      base_url="https://kymaapi.com/v1",
      api_key="ky-your-api-key"
  )

  tools = [{"type": "function", "function": {
      "name": "run_python", "description": "Execute Python code and return the output",
      "parameters": {"type": "object", "properties": {
          "code": {"type": "string"}}, "required": ["code"]}}}]

  def run_python(code: str) -> str:
      import subprocess
      r = subprocess.run(["python3", "-c", code], capture_output=True, text=True, timeout=10)
      return r.stdout or r.stderr

  messages = [
      {"role": "system", "content": "You are a coding assistant. Use run_python to test your code."},
      {"role": "user", "content": "Write a function to find all prime numbers up to 50."}
  ]
  while True:
      response = client.chat.completions.create(
          model="qwen-3-coder", messages=messages, tools=tools, temperature=0)
      msg = response.choices[0].message
      messages.append(msg)
      if response.choices[0].finish_reason != "tool_calls":
          print(msg.content); break
      for call in msg.tool_calls:
          result = run_python(json.loads(call.function.arguments)["code"])
          messages.append({"role": "tool", "tool_call_id": call.id, "content": result})
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from "openai";
  import { execSync } from "child_process";

  const client = new OpenAI({
    baseURL: "https://kymaapi.com/v1",
    apiKey: "ky-your-api-key",
  });

  const tools = [{
    type: "function",
    function: {
      name: "run_python",
      description: "Execute Python code and return the output",
      parameters: {
        type: "object",
        properties: { code: { type: "string" } },
        required: ["code"],
      },
    },
  }];

  const messages = [
    { role: "system", content: "You are a coding assistant. Use run_python to test your code." },
    { role: "user", content: "Write a function to find all prime numbers up to 50." },
  ];

  while (true) {
    const response = await client.chat.completions.create({
      model: "qwen-3-coder", messages, tools, temperature: 0,
    });
    const msg = response.choices[0].message;
    messages.push(msg);
    if (response.choices[0].finish_reason !== "tool_calls") {
      console.log(msg.content);
      break;
    }
    for (const call of msg.tool_calls) {
      const code = JSON.parse(call.function.arguments).code;
      const result = execSync(`python3 -c "${code.replace(/"/g, '\\"')}"`, { encoding: "utf8" });
      messages.push({ role: "tool", tool_call_id: call.id, content: result });
    }
  }
  ```
</CodeGroup>

## Tips & Best Practices

* **Set `temperature=0`** — deterministic code generation reduces syntax errors and off-script behavior.
* **Include file context in system prompt** — paste relevant file contents so the model knows your codebase structure.
* **All active models support tool calling** — check `supports_tools` in `/v1/models` if adding new models.
* **Limit execution environment** — sandbox `run_python` with a timeout and restricted imports for production agents.

## Cost Estimate

| Agent task              | Steps      | Model          | Cost          |
| ----------------------- | ---------- | -------------- | ------------- |
| Write + test a function | 2-3 steps  | `qwen-3-coder` | \~\$0.06–0.09 |
| Debug + fix a bug       | 3-5 steps  | `kimi-k2.6`    | \~\$0.15–0.25 |
| Implement a feature     | 5-10 steps | `minimax-m2.5` | \~\$0.20–0.40 |

Costs scale with context size — long files passed to the model increase input tokens significantly.

## Next Steps

* [Tool Calling](/guides/tool-calling) — complete function calling reference
* [Agent Setup](/guides/agent-setup) — connect Cline, Roo Code, and other agents
* [Model Aliases](/guides/model-aliases) — use `code` or `agent` as shorthand model IDs
