{"product_id":"extended-thinking-tool-use-suy-luận-sau-kết-hợp-cong-cụ","title":"Extended Thinking + Tool Use — Suy luận sâu kết hợp công cụ","description":"\n\u003cp\u003eNếu Extended Thinking giúp Claude \"suy nghĩ to\", thì kết hợp với Tool Use tạo ra một combination cực kỳ mạnh mẽ: Claude không chỉ suy luận mà còn \u003cstrong\u003ebiết khi nào cần gọi tool\u003c\/strong\u003e, gọi tool nào, và truyền tham số gì. Kết quả: agent thông minh hơn, ít lỗi hơn, và dễ debug hơn.\u003c\/p\u003e\n\n\u003ch2\u003eVấn đề với Tool Use thông thường\u003c\/h2\u003e\n\n\u003cp\u003eKhi dùng Tool Use mà không có Extended Thinking, Claude đôi khi:\u003c\/p\u003e\n\n\u003cul\u003e\n  \u003cli\u003eGọi sai tool cho tình huống (nhầm tool search với tool calculator)\u003c\/li\u003e\n  \u003cli\u003eTruyền tham số thiếu hoặc sai format\u003c\/li\u003e\n  \u003cli\u003eGọi nhiều tool không cần thiết (over-calling)\u003c\/li\u003e\n  \u003cli\u003eBỏ qua tool khi nên dùng (under-calling)\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003cp\u003eExtended Thinking giải quyết điều này bằng cách cho Claude \"suy nghĩ trước\" về strategy — trước khi commit vào bất kỳ tool call nào.\u003c\/p\u003e\n\n\u003ch2\u003eSetup: Định nghĩa Tools\u003c\/h2\u003e\n\n\u003cp\u003eĐầu tiên định nghĩa các tools. Ví dụ: một agent phân tích tài chính:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003eimport anthropic\nimport json\n\nclient = anthropic.Anthropic()\n\ntools = [\n    {\n        \"name\": \"get_stock_price\",\n        \"description\": \"Lấy giá cổ phiếu hiện tại và lịch sử. Dùng khi cần dữ liệu giá thực tế.\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"symbol\": {\n                    \"type\": \"string\",\n                    \"description\": \"Mã cổ phiếu, ví dụ: AAPL, MSFT, VNM\"\n                },\n                \"period\": {\n                    \"type\": \"string\",\n                    \"enum\": [\"1d\", \"1w\", \"1m\", \"3m\", \"1y\"],\n                    \"description\": \"Khoảng thời gian lịch sử\"\n                }\n            },\n            \"required\": [\"symbol\"]\n        }\n    },\n    {\n        \"name\": \"calculate_metrics\",\n        \"description\": \"Tính toán các chỉ số tài chính: P\/E ratio, ROE, CAGR. Dùng khi có số liệu thô cần tính toán.\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"metric\": {\n                    \"type\": \"string\",\n                    \"enum\": [\"pe_ratio\", \"roe\", \"cagr\", \"volatility\"]\n                },\n                \"values\": {\n                    \"type\": \"array\",\n                    \"items\": {\"type\": \"number\"},\n                    \"description\": \"Dãy số liệu đầu vào\"\n                },\n                \"period_years\": {\n                    \"type\": \"number\",\n                    \"description\": \"Số năm (cho CAGR)\"\n                }\n            },\n            \"required\": [\"metric\", \"values\"]\n        }\n    },\n    {\n        \"name\": \"search_news\",\n        \"description\": \"Tìm tin tức gần đây về công ty hoặc thị trường. Dùng khi cần thông tin định tính, sentiment.\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"query\": {\"type\": \"string\"},\n                \"days_back\": {\"type\": \"integer\", \"default\": 7}\n            },\n            \"required\": [\"query\"]\n        }\n    }\n]\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eKích hoạt Extended Thinking + Tools\u003c\/h2\u003e\n\n\u003cpre\u003e\u003ccode\u003edef analyze_with_thinking(user_question):\n    messages = [{\"role\": \"user\", \"content\": user_question}]\n\n    response = client.messages.create(\n        model=\"claude-opus-4-5\",\n        max_tokens=20000,\n        thinking={\n            \"type\": \"enabled\",\n            \"budget_tokens\": 12000\n        },\n        tools=tools,\n        messages=messages\n    )\n\n    return response\n\nquestion = \"\"\"\nTôi đang xem xét đầu tư vào AAPL. Hãy phân tích:\n1. Xu hướng giá 3 tháng gần đây\n2. Tính P\/E ratio dựa trên giá hiện tại (EPS = 6.57)\n3. Tin tức quan trọng gần đây\nCho tôi khuyến nghị mua\/giữ\/bán.\n\"\"\"\n\nresponse = analyze_with_thinking(question)\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eXử lý Tool Call Loop với Thinking\u003c\/h2\u003e\n\n\u003cp\u003ePhần phức tạp nhất là xử lý agentic loop — Claude có thể gọi nhiều tools liên tiếp:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003edef run_agent_loop(user_question):\n    messages = [{\"role\": \"user\", \"content\": user_question}]\n    all_thinking = []\n\n    while True:\n        response = client.messages.create(\n            model=\"claude-opus-4-5\",\n            max_tokens=20000,\n            thinking={\"type\": \"enabled\", \"budget_tokens\": 8000},\n            tools=tools,\n            messages=messages\n        )\n\n        # Thu thập thinking blocks để debug\n        for block in response.content:\n            if block.type == \"thinking\":\n                all_thinking.append(block.thinking)\n                print(f\"\n[Thinking #{len(all_thinking)}]\")\n                print(block.thinking[:500] + \"...\" if len(block.thinking) \u0026gt; 500 else block.thinking)\n\n        # Kiểm tra stop reason\n        if response.stop_reason == \"end_turn\":\n            # Claude đã xong, không cần gọi thêm tool\n            final_text = next(\n                (b.text for b in response.content if b.type == \"text\"),\n                \"\"\n            )\n            return final_text, all_thinking\n\n        elif response.stop_reason == \"tool_use\":\n            # Claude muốn gọi tools — xử lý và tiếp tục\n            tool_results = []\n\n            for block in response.content:\n                if block.type == \"tool_use\":\n                    print(f\"\n[Tool Call: {block.name}]\")\n                    print(f\"Input: {json.dumps(block.input, ensure_ascii=False)}\")\n\n                    # Gọi tool thực tế\n                    result = execute_tool(block.name, block.input)\n                    print(f\"Result: {result}\")\n\n                    tool_results.append({\n                        \"type\": \"tool_result\",\n                        \"tool_use_id\": block.id,\n                        \"content\": str(result)\n                    })\n\n            # Thêm response + results vào message history\n            messages.append({\"role\": \"assistant\", \"content\": response.content})\n            messages.append({\"role\": \"user\", \"content\": tool_results})\n\n        else:\n            break\n\n    return None, all_thinking\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eImplement Tool Execution\u003c\/h2\u003e\n\n\u003cpre\u003e\u003ccode\u003edef execute_tool(tool_name, tool_input):\n    \"\"\"Mock implementations — thay bằng real APIs trong production\"\"\"\n\n    if tool_name == \"get_stock_price\":\n        symbol = tool_input[\"symbol\"]\n        period = tool_input.get(\"period\", \"1m\")\n        # Trong production: gọi Yahoo Finance, Alpha Vantage, etc.\n        mock_data = {\n            \"symbol\": symbol,\n            \"current_price\": 189.30,\n            \"period\": period,\n            \"prices\": [175.2, 178.4, 182.1, 185.6, 189.3],\n            \"change_pct\": 8.04\n        }\n        return json.dumps(mock_data)\n\n    elif tool_name == \"calculate_metrics\":\n        metric = tool_input[\"metric\"]\n        values = tool_input[\"values\"]\n\n        if metric == \"pe_ratio\":\n            price, eps = values[0], values[1]\n            return {\"pe_ratio\": round(price \/ eps, 2)}\n\n        elif metric == \"cagr\":\n            start, end = values[0], values[-1]\n            years = tool_input.get(\"period_years\", 1)\n            cagr = ((end \/ start) ** (1 \/ years) - 1) * 100\n            return {\"cagr\": round(cagr, 2)}\n\n    elif tool_name == \"search_news\":\n        query = tool_input[\"query\"]\n        # Trong production: gọi News API, Google News, etc.\n        return {\n            \"articles\": [\n                {\"title\": f\"Apple reports strong Q4 results\", \"sentiment\": \"positive\"},\n                {\"title\": f\"iPhone 16 demand exceeds expectations\", \"sentiment\": \"positive\"},\n                {\"title\": f\"Competition from Samsung intensifies\", \"sentiment\": \"neutral\"}\n            ]\n        }\n\n    return {\"error\": f\"Unknown tool: {tool_name}\"}\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eQuan sát Thinking trong Tool Selection\u003c\/h2\u003e\n\n\u003cp\u003ePhần thú vị nhất: nhìn vào thinking để thấy Claude \"lý luận\" về việc dùng tool nào:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003e# Chạy agent và in thinking\nfinal_answer, thinking_log = run_agent_loop(question)\n\nprint(\"\n\" + \"=\"*60)\nprint(\"THINKING ANALYSIS:\")\nprint(\"=\"*60)\nfor i, thought in enumerate(thinking_log, 1):\n    print(f\"\n--- Thinking Round {i} ---\")\n    print(thought)\n\nprint(\"\n\" + \"=\"*60)\nprint(\"FINAL ANSWER:\")\nprint(\"=\"*60)\nprint(final_answer)\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003cp\u003eTypical thinking output trông như sau:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003e--- Thinking Round 1 ---\nNgười dùng muốn phân tích cổ phiếu AAPL. Tôi cần:\n1. Lấy dữ liệu giá 3 tháng -\u0026gt; dùng get_stock_price(AAPL, 3m)\n2. Tính P\/E ratio -\u0026gt; cần giá hiện tại từ bước 1, EPS đã cho (6.57)\n   -\u0026gt; dùng calculate_metrics(pe_ratio, [price, 6.57])\n3. Tìm tin tức -\u0026gt; dùng search_news(\"Apple AAPL stock\")\nTôi sẽ gọi get_stock_price trước để có giá, sau đó tính metrics.\n\n--- Thinking Round 2 ---\nĐã có giá AAPL: 189.30, tăng 8.04% trong 3 tháng.\nBây giờ tính P\/E: 189.30 \/ 6.57 = 28.81 — hơi cao so với industry average ~25.\nCũng cần tin tức để có context đầy đủ cho recommendation.\n\n--- Thinking Round 3 ---\nCó đủ data:\n- Giá tăng 8%: tích cực\n- P\/E 28.81: slightly premium nhưng justified với growth\n- News: 2\/3 positive, 1 neutral\nKhuyến nghị: BUY với target price 195, stop loss 180.\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eBest Practices\u003c\/h2\u003e\n\n\u003ch3\u003e1. Tool Description phải cực kỳ rõ ràng\u003c\/h3\u003e\n\u003cp\u003eThinking giúp Claude chọn đúng tool hơn, nhưng chỉ khi description đủ tốt:\u003c\/p\u003e\n\n\u003cpre\u003e\u003ccode\u003e# Tệ\n{\"name\": \"search\", \"description\": \"Search something\"}\n\n# Tốt\n{\n    \"name\": \"search_knowledge_base\",\n    \"description\": \"Tìm kiếm trong knowledge base nội bộ của công ty. Dùng khi câu hỏi liên quan đến policy, procedure, hoặc thông tin nội bộ. KHÔNG dùng cho thông tin real-time hoặc dữ liệu bên ngoài.\"\n}\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch3\u003e2. Budget Tokens phù hợp với độ phức tạp\u003c\/h3\u003e\n\n\u003cpre\u003e\u003ccode\u003e# Đơn giản: ít tools, query rõ ràng\nthinking={\"type\": \"enabled\", \"budget_tokens\": 3000}\n\n# Phức tạp: nhiều tools, cần strategy\nthinking={\"type\": \"enabled\", \"budget_tokens\": 12000}\n\n# Research-level: multi-step, uncertain path\nthinking={\"type\": \"enabled\", \"budget_tokens\": 25000}\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch3\u003e3. Log thinking để debug agent behavior\u003c\/h3\u003e\n\n\u003cpre\u003e\u003ccode\u003eimport logging\n\ndef log_agent_step(step_num, thinking, tool_name=None, tool_result=None):\n    logging.info(f\"Step {step_num} | Tool: {tool_name}\")\n    logging.debug(f\"Thinking: {thinking[:200]}...\")\n    if tool_result:\n        logging.info(f\"Tool result: {str(tool_result)[:100]}...\")\u003c\/code\u003e\u003c\/pre\u003e\n\n\u003ch2\u003eSo sánh: Với và không có Extended Thinking\u003c\/h2\u003e\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n\u003cth\u003eTình huống\u003c\/th\u003e\n\u003cth\u003eKhông có Thinking\u003c\/th\u003e\n\u003cth\u003eVới Thinking\u003c\/th\u003e\n\u003c\/tr\u003e\n  \u003c\/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n\u003ctd\u003eTool selection accuracy\u003c\/td\u003e\n\u003ctd\u003e~80%\u003c\/td\u003e\n\u003ctd\u003e~95%\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eParameter correctness\u003c\/td\u003e\n\u003ctd\u003e~85%\u003c\/td\u003e\n\u003ctd\u003e~98%\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eUnnecessary tool calls\u003c\/td\u003e\n\u003ctd\u003eThường xuyên\u003c\/td\u003e\n\u003ctd\u003eHiếm\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eLatency\u003c\/td\u003e\n\u003ctd\u003eThấp hơn\u003c\/td\u003e\n\u003ctd\u003eCao hơn (~2-5x)\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eToken cost\u003c\/td\u003e\n\u003ctd\u003eThấp hơn\u003c\/td\u003e\n\u003ctd\u003eCao hơn (thinking tokens)\u003c\/td\u003e\n\u003c\/tr\u003e\n    \u003ctr\u003e\n\u003ctd\u003eDebuggability\u003c\/td\u003e\n\u003ctd\u003eKhó\u003c\/td\u003e\n\u003ctd\u003eDễ (đọc thinking)\u003c\/td\u003e\n\u003c\/tr\u003e\n  \u003c\/tbody\u003e\n\u003c\/table\u003e\n\n\u003ch2\u003eTổng kết\u003c\/h2\u003e\n\n\u003cp\u003eExtended Thinking + Tool Use là combination lý tưởng cho \u003cstrong\u003ecomplex agentic tasks\u003c\/strong\u003e. Claude không chỉ biết dùng tools mà còn \"lý luận\" về strategy tổng thể trước khi hành động.\u003c\/p\u003e\n\n\u003cp\u003eKhi nào nên dùng combination này:\u003c\/p\u003e\n\u003cul\u003e\n  \u003cli\u003eAgent có 3+ tools với overlap về use case\u003c\/li\u003e\n  \u003cli\u003eTask đòi hỏi multi-step planning\u003c\/li\u003e\n  \u003cli\u003eCorrectness quan trọng hơn speed\u003c\/li\u003e\n  \u003cli\u003eCần khả năng debug và explain decisions\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003cp\u003eKhám phá tiếp: \u003ca href=\"\/collections\/nang-cao\"\u003eAgent Workflows — Chaining, Routing, Parallelization\u003c\/a\u003e để xây dựng hệ thống multi-agent phức tạp hơn.\u003c\/p\u003e\n\n\u003chr\u003e\n\u003ch3\u003eBài viết liên quan\u003c\/h3\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca href=\"\/products\/extended-thinking-d%E1%BB%83-claude-suy-lu%E1%BA%ADn-t%E1%BB%ABng-b%C6%B0%E1%BB%9Bc-minh-b%E1%BA%A1ch\"\u003eExtended Thinking — Để Claude suy luận từng bước minh bạch\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/products\/calculator-tool-bai-h%E1%BB%8Dc-d%E1%BA%A7u-tien-v%E1%BB%81-tool-use-v%E1%BB%9Bi-claude\"\u003eCalculator Tool — Bài học đầu tiên về Tool Use với Claude\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/products\/tim-ki%E1%BA%BFm-wikipedia-v%E1%BB%9Bi-claude-research-agent-d%C6%A1n-gi%E1%BA%A3n\"\u003eTìm kiếm Wikipedia với Claude — Research agent đơn giản\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/products\/extended-thinking-ultrathink-khai-thac-suy-lu%E1%BA%ADn-sau-c%E1%BB%A7a-claude\"\u003eExtended Thinking \u0026amp; Ultrathink — Khai thác suy luận sâu của Claude\u003c\/a\u003e\u003c\/li\u003e\n\u003cli\u003e\u003ca href=\"\/products\/autonomous-coding-agent-ai-t%E1%BB%B1-vi%E1%BA%BFt-code-t%E1%BB%AB-spec\"\u003eAutonomous Coding Agent — AI tự viết code từ spec\u003c\/a\u003e\u003c\/li\u003e\n\u003c\/ul\u003e","brand":"Minh Tuấn","offers":[{"title":"Default Title","offer_id":47721896607956,"sku":null,"price":0.0,"currency_code":"VND","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0821\/0264\/9044\/files\/extended-thinking-tool-use-suy-lu_n-sau-k_t-h_p-cong-c_304b579e-e0af-41fb-bf25-1c212c3df086.jpg?v=1774521756","url":"https:\/\/claude.vn\/products\/extended-thinking-tool-use-suy-lu%e1%ba%adn-sau-k%e1%ba%bft-h%e1%bb%a3p-cong-c%e1%bb%a5","provider":"CLAUDE.VN","version":"1.0","type":"link"}