
تبني الـ Agent بإيه: Messages API ولا Tool Runner ولا Agent SDK ولا Managed Agents؟
- كاتب موثّق
- Claude API
محتويات المقال
read_file وgrep وedit_file، وchecks على الـ paths عشان الـ agent ميلمسش ملفات برّه الـ repo، وحد للـ iterations، وfix لليوم اللي log طويل فجّر فيه الـ context. وفي الـ review، حد سأل ليه مستخدموش الـ Agent SDK، اللي فيه الـ tools دي والـ permission rules وإدارة الـ context جاهزين.
الفكرة الغلط الشائعة
كل tutorial عن الـ tool use بيبدأ بالـ loop اليدوي، لأن ده فعلًا اللي الـ protocol بيشتغل بيه. والـ فعلًا loop: تنادي الموديل، تشغّل الـ tools اللي طلبها، وتكرر لحد ما يخلص. والـ agent frameworks أذت ناس قبل كده، بـ prompts مستخبية وretries مش واضحة وdebugging متعب. وإنك تمسك الـ loop بإيدك شكله الطريقة الوحيدة إنك تتحكم في الموافقات والـ logging والتكلفة.
اللي بيحصل فعلًا
سؤالين بيحددوا الطبقة
الـ Messages API: انت مسؤول عن كل حاجة
stop_reason، وتشغّل كل tool_use block، وترجّع tool_result blocks، وتكرر. انت اللي بتقرر كل تفصيلة، وده بالظبط السبب إن docs الـ tool runner نفسها بتبعتك هنا "When you need human-in-the-loop approval, custom logging, or conditional execution". وانت كمان مسؤول عن كل الحالات الصعبة: نتايج الـ في رسالة واحدة، والنهايات بـ max_tokens وpause_turn، والـ thinking blocks اللي لازم ترجع زي ما هي، وhistory بيكبر مع كل turn.
الـ Tool Runner: الـ SDK بيشغّل الـ loop على الـ tools بتاعتك
@beta_tool بيطلّع الـ JSON schema لكل tool من الـ type hints والـ docstring بتوع الـ function. ولما tool يرمي exception، الـ runner بيرجّع الـ error لـ Claude كـ tool result فيه is_error: true. وبرضه تقدر تلف على كل رسالة، وتحط سقف للتشغيل بـ max_iterations، وتشوف أو تعدّل نتايج الـ tools قبل ما تتبعت، أو تمسك الـ history بإيدك في turn معيّن. بس ملوش tools خاصة بيه: كل tool هي function انت كاتبها وشغالة عندك.
الـ Agent SDK: الـ harness بتاع Claude Code جوه الـ process بتاعك
Glob وGrep، وبتشغّل Bash وبتستخدم الويب. وحواليها عندك permission rules، وhooks، وsubagents، وMCP servers، وsessions تقدر تكمّلها بعدين، وlimits زي max_turns وmax_budget_usd. ولو الـ agent بتاعك شغال على ملفات وcommands، فأغلب شغل التلات أسابيع بتوع الـ CI doctor جاهز.
الـ Managed Agents: Anthropic بتشغّل الـ harness والـ sandbox
نفس الـ agent على طبقتين
import anthropicclient = anthropic.Anthropic()messages = [{"role": "user", "content": "Why did build 812 fail?"}]while True: # loop, schemas, dispatch and error wrapping: all ours reply = client.messages.create( model="claude-opus-5-5", max_tokens=4096, tools=TOOL_SCHEMAS, messages=messages, # JSON schemas by hand ) messages.append({"role": "assistant", "content": reply.content}) if reply.stop_reason != "tool_use": # max_tokens? pause_turn? ours too break results = [] for call in [b for b in reply.content if b.type == "tool_use"]: try: output, failed = HANDLERS[call.name](**call.input), False except Exception as err: output, failed = str(err), True results.append({"type": "tool_result", "tool_use_id": call.id, "content": output, "is_error": failed}) messages.append({"role": "user", "content": results})print(next(b.text for b in reply.content if b.type == "text"))import anthropicfrom anthropic import beta_toolclient = anthropic.Anthropic()@beta_tooldef read_build_log(build_id: int) -> str: """Return the last 200 lines of a CI build log. Args: build_id: The CI build number. """ return fetch_log(build_id) # an exception becomes an is_error resultrunner = client.beta.messages.tool_runner( model="claude-opus-5-5", max_tokens=4096, max_iterations=10, tools=[read_build_log], messages=[{"role": "user", "content": "Why did build 812 fail?"}],)final = runner.until_done()print(next(b.text for b in final.content if b.type == "text"))import asynciofrom claude_agent_sdk import ClaudeAgentOptions, ResultMessage, queryasync def main(): async for message in query( prompt="Build 812 failed. Find the cause in this repo and fix it.", options=ClaudeAgentOptions( allowed_tools=["Read", "Glob", "Grep", "Edit"], # built in permission_mode="acceptEdits", max_turns=30, max_budget_usd=5.0, ), ): if isinstance(message, ResultMessage): print(message.subtype) # success, error_max_turns, ...asyncio.run(main())تطبقها إزاي في مشروعك
اكتب الـ agent محتاج إيه الـ tools بتاعته، وهل بيلمس ملفات أو shell، والتشغيل بياخد قد إيه، ومين يدير الجهاز. جاوب على السؤالين حدد مين يشغّل الـ loop والـ tools تشتغل فين، والجدول اللي فوق بيوصّلك للطبقة. صغّر الـ loops المكتوبة بالإيد على tools خاصة بيك لو الـ loop بتاعك بس بينادي functions انت كاتبها، جرّب الـ tool runner، وخلّي الكود الخاص للـ turns اللي محتاجاه. متبنيش tools بتاعة Claude Code من الأول قبل ما تكتب tools للملفات أو البحث أو الـ shell، جرّب الـ Agent SDK بـ allowed_toolsوpermission_mode.حط limits واقرا التشغيل خلص إزاي استخدم max_iterations، أوmax_turnsوmax_budget_usd، وشوف الـstop_reasonالأخير أو الـ subtype بتاعResultMessageبدل ما تفترض إنه نجح.
الخلاصة
الـ agent عبارة عن loop، بس الـ loop هو أصغر جزء في الشغل. اختار الطبقة على حسب مين يشغّل الـ loop والـ tools تشتغل فين. الـ tool runner بيشيل الـ loop عن الـ tools بتاعتك، وبرضه بيسيبك تتدخل في أي turn. الـ Agent SDK بيديك الـ tools والـ permissions وإدارة الـ context بتوع Claude Code في process انت بتشغّله. الـ Managed Agents بتستضيف الـ harness والـ sandbox؛ راجع شروط الاحتفاظ بالداتا قبل ما تعتمد عليها.
التعليقات (0)