시스템 프롬프트 재주입

시스템 프롬프트 재주입 (Reinject System Prompt)

ReinjectSystemPrompt은 에이전트에 설정된 system_prompt이 매 모델 요청의 첫 번째 ModelRequest 맨앞에 오도록 보장하는 캐퍼빌리티예요. 히스토리에서 시스템 프롬프트가 빠질 수 있는 다양한 상황을 안전하게 다루는 데 유용합니다.

출처: 문서

본문

기본적으로 히스토리에 SystemPromptPart가 이미 있다면 이 캐퍼빌리티는 아무 일도 하지 않아요 (그래서 멀티 에이전트 핸드오프와 사용자 관리 시스템 프롬프트가 권위를 유지합니다). replace_existing=True를 설정하면 기존 SystemPromptPart들을 제거한 뒤 에이전트가 설정한 프롬프트를 앞에 추가합니다 — 히스토리가 신뢰할 수 없는 출처에서 와서 서버의 프롬프트가 이겨야 할 때 유용해요.

기본 id'reinject_system_prompt'를 선언하므로, 두 인스턴스는 중복 id 오류를 내는 대신 병합됩니다 — 병합 규칙은 커스텀 캐퍼빌리티 만들기를 참고하세요.

message_history가 시스템 프롬프트를 왕복시키지 않는 출처에서 올 때 유용해요 — UI 프런트엔드, 데이터베이스 영속 계층, 대화 압축 파이프라인 같은 경우요. 이 캐퍼빌리티가 없으면 system_prompt가 설정된 에이전트는 히스토리에 시스템 프롬프트가 없을 때 조용히 그것 없이 실행됩니다.

from pydantic_ai import Agent
from pydantic_ai.capabilities import ReinjectSystemPrompt
from pydantic_ai.messages import ModelRequest, ModelResponse, TextPart, UserPromptPart

agent = Agent('test', system_prompt='You are a helpful assistant.', capabilities=[ReinjectSystemPrompt()])

# History that's missing the system prompt (e.g. reconstructed from a UI frontend).
history = [
    ModelRequest(parts=[UserPromptPart(content='Hi')]),
    ModelResponse(parts=[TextPart(content='Hello!')]),
]

# Without the capability, the agent would run without its configured system prompt.
# With the capability, the system prompt is reinjected at the head of the first request.
result = agent.run_sync('Follow up', message_history=history)
first_request = result.all_messages()[0]
assert isinstance(first_request, ModelRequest)
assert first_request.parts[0].content == 'You are a helpful assistant.'

(이 예제는 완전하며, "그대로" 실행할 수 있어요)

UI 어댑터 (AG-UI, Vercel AI)는 manage_system_prompt='server' 모드에서 이 캐퍼빌리티를 replace_existing=True로 자동 추가합니다.

더 알아보기 (Learn more)