> ## 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.

# Agent Backend

> Build an AI agent backend with tool calling, multi-step reasoning, and conversation loops.

## Best Model

**Kimi K2.5** (`kimi-k2.6`) — Built for agentic workflows with strong function calling and 262K context. \~\$1.09 per 1K requests.

For budget agents, use **DeepSeek V3** (`deepseek-v3`) at \~\$0.86 per 1K requests.

## Python — Agent with Tool Loop

```python theme={null}
from openai import OpenAI
import json

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

tools = [
    {
        "type": "function",
        "function": {
            "name": "search_docs",
            "description": "Search documentation for a query",
            "parameters": {
                "type": "object",
                "properties": {"query": {"type": "string"}},
                "required": ["query"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "run_code",
            "description": "Execute Python code and return output",
            "parameters": {
                "type": "object",
                "properties": {"code": {"type": "string"}},
                "required": ["code"]
            }
        }
    }
]

def handle_tool_call(name, args):
    if name == "search_docs":
        return f"Found: documentation about {args['query']}"
    if name == "run_code":
        return f"Output: executed successfully"
    return "Unknown tool"

def run_agent(user_message, max_steps=5):
    messages = [
        {"role": "system", "content": "You are a helpful agent. Use tools to answer questions."},
        {"role": "user", "content": user_message}
    ]

    for _ in range(max_steps):
        response = client.chat.completions.create(
            model="kimi-k2.6",
            messages=messages,
            tools=tools,
        )

        choice = response.choices[0]
        messages.append(choice.message)

        if choice.finish_reason == "stop":
            return choice.message.content

        if choice.message.tool_calls:
            for call in choice.message.tool_calls:
                args = json.loads(call.function.arguments)
                result = handle_tool_call(call.function.name, args)
                messages.append({
                    "role": "tool",
                    "tool_call_id": call.id,
                    "content": result
                })

    return messages[-1].content

print(run_agent("Search docs for authentication and write example code"))
```

## JavaScript — Express Agent API

```typescript theme={null}
import OpenAI from "openai";
import express from "express";

const client = new OpenAI({
  baseURL: "https://kymaapi.com/v1",
  apiKey: process.env.KYMA_API_KEY,
});

const app = express();
app.use(express.json());

app.post("/agent", async (req, res) => {
  const { message, history = [] } = req.body;

  const messages = [
    { role: "system", content: "You are a helpful agent with tool access." },
    ...history,
    { role: "user", content: message },
  ];

  const response = await client.chat.completions.create({
    model: "kimi-k2.6",
    messages,
    tools: [/* your tools here */],
  });

  res.json({
    reply: response.choices[0].message.content,
    tool_calls: response.choices[0].message.tool_calls,
  });
});

app.listen(3000);
```

## Tips

* Use `kimi-k2.6` for complex multi-step agents, `deepseek-v3` for simpler ones
* Keep tool descriptions clear and concise — the model uses them to decide when to call
* Set `max_steps` to prevent infinite loops
* Store conversation history for multi-turn agent sessions

## Cost Estimate

| Scenario                   | Tokens          | Model     | Cost      |
| -------------------------- | --------------- | --------- | --------- |
| Simple tool call (1 step)  | 1K in / 500 out | kimi-k2.6 | \~\$0.003 |
| Multi-step agent (3 steps) | 5K in / 2K out  | kimi-k2.6 | \~\$0.01  |
| Complex workflow (5 steps) | 15K in / 5K out | kimi-k2.6 | \~\$0.03  |

## Next Steps

* [Tool Calling Guide](/guides/tool-calling) — detailed function calling reference
* [Structured Outputs](/guides/structured-outputs) — force JSON schema responses
* [Coding Agent Use Case](/guides/use-cases/coding-agent) — specialized coding agent
