# Agents: Tool-Calling Loop — LangChain / LlamaIndex

Source: https://www.geekswithgeeks.com/hi/langchain-llamaindex/g-agent

> समझें कि agent कैसे निर्णय लेता, कार्य करता और देखता है।

## निर्णय, कार्य, अवलोकन, दोहराएँ

**Agent** tools के साथ loop में चलता LLM है। हर turn मॉडल या तो अंतिम उत्तर लौटाता है या **tool call** माँगता है; आपका कोड tool चलाता है, **परिणाम** को संदेश के रूप में जोड़ता है और मॉडल को फिर बुलाता है, जब तक वह उत्तर न दे या सीमा न आ जाए। Agent बस इतना ही है; frameworks loop, tool schemas, संदेश formats और सहायक देते हैं। Agents तब उपयोग करें जब चरणों की संख्या और क्रम पहले से न जाने जा सकें (जैसे शोध, debugging, बहु-स्रोत सवाल)। निश्चित क्रमों के लिए सादी chain अधिक सरल, सस्ती और अनुमेय है। हमेशा **अधिकतम iterations**, **token और समय बजट** रखें और हर tool call log करें।

## मॉडल जो कार्रवाइयाँ चुनते हैं

Agent सोचने और कार्य करने के बीच loop करता है; workflows उस loop पर नियंत्रण देते हैं।

![तीन विचार: loop, graph, guardrails।](assets/figures/langchain-llamaindex/section-6-map.svg) — चित्र 6.1 — Loop, graph और guardrails।

## Pseudocode में loop

Frameworks इसे लपेटते हैं; loop जानना debug में मदद करता है। उदाहरण; यहाँ चलाया नहीं गया।

```python
messages = [system, user_question]
for step in range(MAX_STEPS):                 # always cap the loop
    reply = model.invoke(messages)             # model sees tools via bind_tools
    messages.append(reply)
    if not reply.tool_calls:                   # no tool requested -> final answer
        return reply.content
    for call in reply.tool_calls:
        result = run_tool(call["name"], call["args"])   # YOUR code validates and executes
        messages.append(tool_message(call["id"], result))
return "Stopped: step limit reached"
```

**Quiz:** हर agent loop में क्या होना चाहिए?

- [x] अधिकतम iterations की सीमा और बजट
- [ ] असीमित retries
- [ ] कोई logging नहीं
- [ ] सब कुछ पर admin पहुँच

*Answer:* अधिकतम iterations की सीमा और बजट. सीमाएँ अनियंत्रित लागत और अंतहीन loops रोकती हैं।
