461 từ
2 phút đọc
OpenAI Agents SDK: Handoffs, Guardrails và An toàn

Các framework chúng ta đã đề cập là open-source hoặc platform-agnostic. OpenAI Agents SDK khác — nó được thiết kế cho models và ecosystem của OpenAI trước tiên. Nhưng những gì nó thiếu về tính di động, nó bù đắp bằng ba lĩnh vực cụ thể mà không framework nào sánh kịp: handoffs, guardrailsbuilt-in safety.

OpenAI ra mắt Agents SDK cuối năm 2025 như người kế nhiệm function calling API và Assistants API. Đây là framework agent thống nhất cho toàn bộ nền tảng OpenAI — GPT, o-series reasoning models, multimodal và tool use.


OpenAI Agents SDK Là Gì?#

Agents SDK là framework Python để xây dựng agents sử dụng OpenAI models. Nó wrap Chat Completions API với các abstraction cấp agent: agents với instructions và tools, handoff giữa các agent, guardrails cho input/output safety và tracing.

from agents import Agent, Runner
agent = Agent(
name="Support Agent",
instructions="Bạn là customer support agent. Hãy hữu ích và ngắn gọn.",
tools=[get_order_status, process_refund]
)
result = Runner.run_sync(agent, "Đơn hàng #12345 của tôi đâu?")
print(result.final_output)

Handoffs: Tính năng Killer#

Handoffs là khả năng nổi bật nhất của SDK. Agent có thể chuyển cuộc hội thoại sang agent khác một cách liền mạch. Agent nhận được toàn bộ conversation context.

from agents import Agent, handoff, Runner
triage_agent = Agent(
name="triage",
instructions="Chuyển đến specialist phù hợp",
handoffs=["billing", "technical", "account"],
)
billing_agent = Agent(
name="billing",
instructions="Xử lý billing và payments",
tools=[process_refund, lookup_invoice],
)
# Handoff với customization
billing_handoff = handoff(
agent=billing_agent,
tool_name_override="transfer_to_billing",
on_handoff=lambda ctx: log_transfer(ctx, "billing"),
)

Khác với function calling. Handoff chuyển conversation state, không chỉ data result.


Guardrails: An toàn Built In#

Guardrails chạy trước và sau mỗi lần gọi agent:

from agents import Guardrail, Runner
class PIIGuardrail(Guardrail):
async def check_input(self, agent, input_data):
if contains_pii(input_data):
return GuardrailResult(
passed=False,
message="Input chứa PII",
transformed=redact_pii(input_data)
)
return GuardrailResult(passed=True)

Built-in guardrails:#

  • PIIGuardrail — Email, phone, SSN, credit card
  • ToxicityGuardrail — Hate speech, harassment
  • ContentGuardrail — Topic restrictions
  • BudgetGuardrail — Token/cost limits
  • ValidationGuardrail — Output schema compliance

Structured Outputs#

from pydantic import BaseModel
from agents import Agent, Runner
class SupportResponse(BaseModel):
summary: str
resolution: str | None
requires_escalation: bool
category: str
agent = Agent(
name="support",
output_type=SupportResponse,
)
result = Runner.run_sync(agent, "Payment failed")

OpenAI SDK vs Các Framework Khác#

Khía cạnhOpenAI SDKLangGraphCrewAIAutoGenClaude SDK
HandoffsBestManual nodesHierarchicalGroupChatKhông
GuardrailsBestCode tùy chỉnhCode tùy chỉnhCode tùy chỉnhCode tùy chỉnh
Structured outputNative PydanticManualManualTyped messagesNative
Multi-modelChỉ OpenAIAny LLMAny LLMAny LLMChỉ Claude
Graph controlLinear chainsFull graphsSequentialConversationsAgent loop
Human-in-loopHạn chếNativeCallbacksCallbacksHooks
TracingDashboardLangSmithCơ bảnAzure MonitorKhông

Khi Nào Chọn#

Phù hợp#

  • Đã dùng OpenAI — GPT-4o, o3, o4-mini
  • Input/output safety critical — Guardrails mature nhất
  • Handoff patterns đơn giản — Support, triage, escalation
  • Structured output bắt buộc — Pydantic best-in-class

Tránh khi#

  • Cần multi-model — Chỉ OpenAI
  • Cần human-in-the-loop mature — LangGraph tốt hơn
  • Cần graph workflow phức tạp — AutoGen/LangGraph tốt hơn
  • Cần self-hosted traces — Chỉ OpenAI dashboard

Tiếp Theo#

BàiFramework
1LangGraph
2CrewAI
3AutoGen
4Claude Agent SDK
5OpenAI Agents SDK (bài này)
6So sánh cuối — coming next

Series: AI Agent Frameworks 2026 — So sánh Production. Bài 5: OpenAI Agents SDK. Finale: So sánh cuối → sắp tới.

Advertisement

OpenAI Agents SDK: Handoffs, Guardrails và An toàn
https://minixium.com/vi/posts/openai-agents-sdk-handoffs-guardrails-safety-2026-vi/
Tác giả
Minixium
Đăng vào lúc
2026-05-24
Giấy phép bản quyền
CC BY-NC-SA 4.0

Advertisement