İçeriğe geç

Ajan döngüsü

Bir kodlama ajanı tek bir istekle çalışmaz: modele araçları verir, modelin istediği aracı çalıştırır, sonucu geri besler ve model bir sonraki adıma karar verir. Bu döngü /v1/responses üzerinde native olarak desteklenir.

Responses API'de konuşma geçmişi input dizisidir. Dizide iki tür öğe bulunur:

  • Rol taşıyan mesajlar{"role": "user", "content": [...]}
  • Native item'lar — rol taşımaz, type ile ayrışır: function_call, function_call_output, reasoning

Her turda modelin ürettiği function_call item'ını ve sizin ürettiğiniz function_call_output item'ını diziye ekleyip isteği tekrarlarsınız.

Terminal window
curl https://llmtr.com/v1/responses \
-H "Authorization: Bearer llmtr-your_key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.3-codex",
"input": [
{"role": "user", "content": [{"type": "input_text", "text": "İstanbul hava durumu?"}]}
],
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Bir şehrin güncel sıcaklığını döndürür.",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
]
}'

Model aracı çağırmaya karar verirse output dizisinde bir function_call item'ı döner:

{
"output": [
{
"type": "function_call",
"call_id": "call_abc123",
"name": "get_weather",
"arguments": "{\"city\":\"Istanbul\"}"
}
]
}

Aracı kendi tarafınızda çalıştırın, sonra önceki input dizisine iki item ekleyin: modelin function_call'ı ve sizin function_call_output'unuz. call_id ikisinde de aynı olmalıdır.

Terminal window
curl https://llmtr.com/v1/responses \
-H "Authorization: Bearer llmtr-your_key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.3-codex",
"input": [
{"role": "user", "content": [{"type": "input_text", "text": "İstanbul hava durumu?"}]},
{
"type": "function_call",
"call_id": "call_abc123",
"name": "get_weather",
"arguments": "{\"city\":\"Istanbul\"}"
},
{
"type": "function_call_output",
"call_id": "call_abc123",
"output": "{\"tempC\":21,\"conditions\":\"acik\"}"
}
],
"tools": [ ... ]
}'

Model artık araç sonucunu görerek yanıt üretir. Döngüyü, model function_call üretmeyi bırakana kadar tekrarlayın.

from openai import OpenAI
client = OpenAI(api_key="llmtr-your_key", base_url="https://llmtr.com/v1")
tools = [{
"type": "function",
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
conversation = [{"role": "user", "content": "İstanbul hava durumu?"}]
while True:
response = client.responses.create(
model="openai/gpt-5.3-codex",
input=conversation,
tools=tools,
)
calls = [item for item in response.output if item.type == "function_call"]
if not calls:
print(response.output_text)
break
# Modelin ürettiği item'ları geçmişe olduğu gibi ekleyin.
conversation += response.output
for call in calls:
result = run_tool(call.name, call.arguments)
conversation.append({
"type": "function_call_output",
"call_id": call.call_id,
"output": result,
})

Reasoning modelleri output içinde reasoning item'ı da döndürebilir. Bu item'ları da geçmişe olduğu gibi ekleyin: model, önceki turlardaki düşünce zincirine erişebildiğinde çok adımlı görevlerde tutarlı kalır. Yukarıdaki conversation += response.output satırı bunu zaten yapar.

Ajan döngüsü stream: true ile de kurulabilir. Araç çağrısı argümanları üretilirken response.function_call_arguments.delta olayları gelir, tamamlanınca response.function_call_arguments.done gelir. Terminal response.completed olayındaki response.output dizisi, non-streaming yanıtla aynı item'ları içerir; döngüyü ondan sürdürebilirsiniz.

Ayrıntı için Responses API sayfasındaki streaming bölümüne bakın.

Model tek turda birden fazla function_call üretebilir. Hepsini çalıştırıp her biri için ayrı bir function_call_output ekleyin. Eksik bırakılan bir call_id sağlayıcı tarafında hataya yol açar.

/v1/chat/completions kullanıyorsanız aynı döngü klasik tool_calls ve role: "tool" mesajlarıyla kurulur; gateway bunları Responses item'larına kendisi çevirir. Ayrıntı için Tool calling sayfasına bakın.

Yalnızca Responses endpoint'ini destekleyen modellerde (GPT-5 Codex serisi, gpt-5.5, gpt-5.6 ailesi) /v1/chat/completions 400 endpoint_mismatch döner; bu modellerde döngüyü /v1/responses üzerinden kurun.

BelirtiSebep
400 invalid_request — input item'ı reddedildiDizideki bir öğe ne role ne type taşıyor
Sağlayıcı "no tool output found" diyorfunction_call eklendi ama eşleşen function_call_output eklenmedi
call_id eşleşmiyorGeri beslenen call_id, modelin döndürdüğüyle birebir aynı olmalı
400 endpoint_mismatchResponses-only model /v1/chat/completions'a gönderildi