LLM 모델 라우팅¶
애플리케이션은
support/primary같은 논리 모델만 선택하고, 운영자는 실제 provider 연결과 모델 ID를 설정으로 교체합니다. 이 가이드는spakky-llm의 model catalog를 직접 구성하거나 환경변수로 주입하고, Python·AG-UI·A2A에서 같은 선택 계약을 사용하는 방법을 설명합니다.
설계 한눈에 보기¶
spakky-llm은 모델 선택을 세 층으로 나눕니다.
flowchart LR
Caller["Caller: ModelSelection(model_ref)"] --> Catalog["models: logical ref catalog"]
Catalog --> Route["LlmModelRoute: physical model + capability"]
Route --> Profile["profiles: endpoint + auth + backend"]
Profile --> Adapter["Official SDK adapter"]
classDef caller fill:#E3F2FD,stroke:#1565C0,color:#0D47A1
classDef catalog fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
classDef config fill:#FFF3E0,stroke:#EF6C00,color:#E65100
classDef external fill:#ECEFF1,stroke:#546E7A,color:#263238
class Caller caller
class Catalog,Route catalog
class Profile config
class Adapter external
| 층 | 소유자 | 담는 값 | 담지 않는 값 |
|---|---|---|---|
ModelSelection |
호출자 | model_ref 하나 |
provider, profile, raw model, credential, endpoint |
LlmModelRoute |
운영자 | profile 참조, 실제 모델 ID, capability, vLLM model option | API key, base URL |
LlmProfile |
운영자 | provider 표식, API family, endpoint, auth, timeout, retry | 실제 모델 ID, model capability |
이 경계 덕분에 애플리케이션 코드는 support/primary를 유지한 채 실제 Anthropic 모델을
Vertex AI나 OpenRouter route로 교체할 수 있습니다. provider는 진단 metadata에 남는
운영자 소유 식별자일 뿐 호출자가 provider를 고르는 입력이 아닙니다.
설치와 기본 동작¶
별도 설정이 없으면 LlmConfig()는 다음 catalog를 만듭니다.
| 항목 | 기본값 | 의미 |
|---|---|---|
default_model |
assistant/default |
선택이 없을 때 사용할 논리 모델 ref |
| route | assistant/default -> vllm-local / default |
실제 provider model은 default |
| profile | vllm-local |
http://127.0.0.1:8000/v1의 OpenAI-compatible vLLM 연결 |
| capability | tools, structured output | 나머지는 ModelCapability 기본값 |
기본값은 로컬 vLLM 서버를 명시적으로 선택합니다. 운영 환경에 맞는 catalog가 있다면
profiles, models, default_model 세 필드를 함께 대체하세요. 일부 필드만 잘못
덮어써 SDK의 ambient 기본값으로 흘러가는 경로는 허용되지 않습니다.
Python에서 직접 구성하기¶
다음은 Anthropic 연결 하나를 support/primary라는 제품 언어 뒤에 숨기는 최소 구성입니다.
from os import environ
from pydantic import SecretStr
from spakky.agent import ModelCapability
from spakky.plugins.llm.config import (
LlmConfig,
LlmModelRoute,
LlmProfile,
LlmProviderApi,
)
config = LlmConfig(
default_model="support/primary",
profiles={
"managed_text": LlmProfile(
provider="anthropic",
api=LlmProviderApi.ANTHROPIC_MESSAGES,
api_key=SecretStr(environ["ANTHROPIC_API_KEY"]),
),
},
models={
"support/primary": LlmModelRoute(
profile="managed_text",
model="claude-opus-4-1",
capability=ModelCapability(
supports_reasoning=True,
supports_tools=True,
supports_structured_output=True,
),
),
},
)
LlmConfig 생성자는 keyword-only이며 공개 입력은 default_model, profiles, models
뿐입니다. LlmProfile과 LlmModelRoute도 알 수 없는 필드를 거부합니다. 따라서 예전
default_profile, profile의 model, 요청별 provider/profile/raw model override는
호환 alias로 남지 않습니다.
Profile timeout과 실행 제한¶
LlmProfile.request_timeout_seconds와 stream_timeout_seconds의 기본값은 모두
None입니다. v8.4.0부터 기존 30초·300초의 기본 timeout을 적용하지 않습니다.
생략하거나 JSON null로 지정하면 해당 profile timeout이 없고, 값을 지정할 때는
양수여야 합니다. 0을 무제한의 별칭으로 사용하지 않습니다.
| 설정 | 범위 | 기본값 |
|---|---|---|
LlmProfile.request_timeout_seconds |
provider의 non-streaming 요청 | None |
LlmProfile.stream_timeout_seconds |
provider의 streaming 요청 | None |
AgentExecutionLimits.timeout_seconds |
runner invocation의 model·async tool await | None |
AgentExecutionLimits.max_steps / max_tool_calls |
같은 run의 model step·tool batch 예산 | 모두 None |
예를 들어 로컬 vLLM profile에 요청별 timeout을 적용하려면 다음처럼 명시합니다. 60초와 180초는 이 예제에서 선택한 값입니다.
from spakky.plugins.llm.config import (
LlmProfile,
LlmProviderApi,
OpenAICompatibleDialect,
)
profile = LlmProfile(
provider="vllm",
api=LlmProviderApi.OPENAI_CHAT_COMPLETIONS,
base_url="http://127.0.0.1:8000/v1",
openai_dialect=OpenAICompatibleDialect.VLLM,
request_timeout_seconds=60.0,
stream_timeout_seconds=180.0,
)
Profile timeout을 생략해도 명시한 runner deadline은 계속 적용됩니다. 반대로 runner deadline을 생략해도 명시한 profile timeout은 남습니다. Runner의 deadline, token/cost 예산과 동기 도구 제한은 AI Agent 심화를 참고하세요. Timeout을 생략하는 것은 진행 중 요청의 취소 계약을 없애는 설정이 아니며, steering에 필요한 cleanup은 실행 중 지시 수정의 adapter 계약을 따릅니다.
환경변수로 구성하기¶
LlmConfig는 SPAKKY_LLM__ prefix와 __ 중첩 구분자를 사용합니다. Profile처럼
환경변수 이름으로 표현하기 쉬운 map은 중첩 key로, /가 들어간 opaque model ref는
JSON object로 전달하면 읽기 쉽고 shell key 제약도 피할 수 있습니다.
export SPAKKY_LLM__DEFAULT_MODEL='support/primary'
export SPAKKY_LLM__PROFILES__MANAGED_TEXT__PROVIDER='anthropic'
export SPAKKY_LLM__PROFILES__MANAGED_TEXT__API='anthropic-messages'
export SPAKKY_LLM__PROFILES__MANAGED_TEXT__API_KEY="$ANTHROPIC_API_KEY"
export SPAKKY_LLM__MODELS='{"support/primary":{"profile":"managed_text","model":"claude-opus-4-1","capability":{"supports_reasoning":true,"supports_tools":true,"supports_structured_output":true}}}'
배포 시스템은 API key나 service-account 파일을 runtime secret 또는 workload identity로
주입해야 합니다. 모델 요청 body, RunAgentInput.metadata, AG-UI/A2A payload는 credential
전달 경로가 아닙니다.
SPAKKY_LLM__DEFAULT_PROFILE 같은 legacy top-level key, PROFILSE 같은 오타, profile이나
route의 알 수 없는 중첩 필드는 시작 단계에서 실패합니다. profiles와 models가 비었거나,
default_model이 catalog에 없거나, route가 없는 profile을 가리켜도 시작할 수 없습니다.
Nested environment map segment는 case-insensitive settings source를 거치며 소문자 key가
됩니다. 위 MANAGED_TEXT segment가 route에서 managed_text를 가리키는 이유입니다.
Support/Primary처럼 case를 보존해야 하는 profile/model key는 PROFILES나 MODELS
전체 JSON object로 전달하세요. Direct Python mapping과 JSON object의 key는 case를
보존합니다.
명시 생성자 인자는 같은 field의 environment source보다 우선하며, env JSON을 decode하기
전에 그 field를 mask합니다. 예를 들어 profiles=...를 직접 넘기면 malformed
SPAKKY_LLM__PROFILES가 있어도 읽지 않지만 MODELS와 DEFAULT_MODEL environment 값은
계속 처리합니다. 세 field를 모두 직접 넘기면 prefixed environment catalog와 unrelated
prefixed key를 읽거나 감사하지 않습니다. Environment JSON을 사용하는 경로에서는
logical ref 중복뿐 아니라 route 내부 중복 key도 모든 depth에서 거부하며
last-key-wins로 처리하지 않습니다.
Ordered fallback과 profile resilience¶
Fallback은 provider 이름에서 추론하지 않습니다. 각 route가 ordered logical refs와 허용할
LlmFailureClass를 함께 선언합니다. Retry, concurrency, rate limit, circuit breaker도
connection profile의 LlmResiliencePolicy를 명시할 때만 켜집니다.
from spakky.plugins.llm.cache import LlmCacheMode, LlmCachePolicy
from spakky.plugins.llm.config import LlmModelRoute
from spakky.plugins.llm.error import LlmFailureClass
from spakky.plugins.llm.resilience import (
LlmCircuitBreakerPolicy,
LlmConcurrencyPolicy,
LlmRateLimitPolicy,
LlmResiliencePolicy,
LlmRetryPolicy,
)
profile_resilience = LlmResiliencePolicy(
retry=LlmRetryPolicy(
max_attempts=2,
backoff_seconds=0.25,
max_backoff_seconds=2.0,
failure_classes=frozenset(
{LlmFailureClass.TIMEOUT, LlmFailureClass.TRANSPORT}
),
),
concurrency=LlmConcurrencyPolicy(
max_in_flight=8,
queue_timeout_seconds=0.5,
),
rate_limit=LlmRateLimitPolicy(
requests_per_period=20,
period_seconds=1.0,
max_wait_seconds=0.2,
),
circuit=LlmCircuitBreakerPolicy(
failure_threshold=3,
recovery_timeout_seconds=30.0,
),
)
primary_route = LlmModelRoute(
profile="managed_text",
model="primary-model",
fallbacks=("support/secondary",),
fallback_on=frozenset(
{
LlmFailureClass.TIMEOUT,
LlmFailureClass.TRANSPORT,
LlmFailureClass.CAPABILITY,
LlmFailureClass.CACHE,
}
),
cache=LlmCachePolicy(mode=LlmCacheMode.EXACT, ttl_seconds=60.0),
)
secondary_route = LlmModelRoute(
profile="secondary_connection",
model="secondary-model",
)
profile_resilience는 해당 LlmProfile(resilience=profile_resilience)에 넣습니다. 모든 default는
disabled입니다: retry는 max_attempts=1, concurrency/rate/circuit limit은 None, cache는
DISABLED, route fallback은 empty입니다. Official SDK의 max_retries>0와 orchestration
max_attempts>1을 동시에 켜 attempt 수를 곱할 수 없습니다.
한 profile의 retry를 소진한 뒤에만 현재 route가 그 failure class에 허용한 fallback edge로 이동합니다. Missing/self/cyclic fallback과 refs/allowlist 중 한쪽만 선언한 route는 config 생성에서 실패합니다. Streaming retry/fallback은 provider event를 하나도 내보내지 않은 실패에만 가능하며 partial stream 뒤에는 다른 billable attempt를 시작하지 않습니다.
Response/error metadata의 attempts, attempted route, ordinal, retry count, fallback source,
failure class, circuit/cache state가 진단 정본입니다. Runner는 nested attempt evidence를
durable model evidence에 남기고 telemetry에는 scalar summary만 보냅니다.
Explicit complete-response cache¶
LlmCacheMode.EXACT와 SEMANTIC은 application이 같은 mode의 ILLMResponseCache와
trusted ILLMCacheScopeResolver를 제공할 때만 쓸 수 있습니다. Enabled route가 필요한
backend 또는 resolver 없이 LlmAgentModel을 만들면 LlmCacheConfigurationError입니다.
Scope의 tenant/safety partition은 arbitrary ModelRequest.metadata key가 아니라 resolver의
authority입니다.
Cache는 complete()의 tool-free response에만 적용됩니다. Streaming은 항상
bypassed_streaming, tool-call response는 store/replay하지 않습니다. EXACT key는 route,
connection, sampling, guarded message/context, tool/schema와 media source/body hash를 묶고 raw
body를 key에 노출하지 않습니다. SEMANTIC backend는 별도의 ordered text/digest
semantic_input을 받으므로 그 입력의 보존·접근 정책은 backend 운영자가 소유합니다.
Media URI authority는 cache lookup보다 먼저 다시 검증됩니다. Cache lookup failure도 현재
route가 LlmFailureClass.CACHE edge를 선언한 경우에만 fallback할 수 있습니다. 반대로
provider success 뒤 cache store가 실패하면 이미 billable 호출이 생겼으므로 fallback을
금지합니다. Typed model error에 usage/routing receipt를 붙여 runner가 token/cost/evidence를
보존한 뒤 종료합니다. Cache hit은 current invocation usage를 0으로 반환하면서 saved usage와
선택 evidence를 metadata에 남깁니다.
Multimodal route와 URI authority¶
Core ImagePart.from_uri() 같은 factory는 network I/O를 하지 않습니다. LlmAgentModel의
default PublicLlmMediaUriPolicy가 각 candidate route에서 cache lookup과 provider I/O보다
먼저 hostname을 off-loop로 resolve하고 timeout/non-public address를
LlmConfigurationError로 거부합니다. ILLMMediaUriPolicy 구현을 constructor-inject해 이
경계를 교체할 수 있습니다.
Part의 MediaSafetyLimits.allowed_uri_hosts는 operator가 부여한 exact host authority이므로
default public-DNS resolution을 생략합니다. 일반 caller metadata나 model ref는 이 allowlist를
늘릴 수 없습니다. Route의 ModelCapability.input_modalities가 text/image/audio/video/document
요청 전체를 지원해야 하고, provider adapter의 MIME/source encoding이 더 좁으면
LlmUnsupportedFeatureError입니다. Capability failure fallback도 route가
LlmFailureClass.CAPABILITY를 명시했을 때만 허용됩니다.
OpenAI, Anthropic, Gemini Developer API, Vertex adapter는 지원하는 input part를 native SDK
shape로 변환합니다. 공통 ModelResponse.content는 아직 text이고 generated non-text output
contract는 없습니다. 가장 짧은 content/attachment 예제는 AI Agent 개발을
확인하세요.
Optional platform ports¶
Interactive ModelRequest와 Agent tool catalog 밖의 provider 기능은 별도 opt-in port입니다.
| Port | Explicit request | 자동으로 하지 않는 일 |
|---|---|---|
ILLMBatchProvider |
LlmBatchRequest submit/status/results |
interactive call batching |
ILLMFileProvider |
LlmFileUpload upload/delete |
upload 또는 prompt injection |
ILLMNativeToolProvider |
WEB_SEARCH/FILE_SEARCH invocation |
Agent tool 등록·자동 실행 |
Core plugin은 payload/port만 제공하며 first-party interactive adapter가 이 기능을 암묵적으로
실행하지 않습니다. Application/provider integration이 구현을 명시적으로 선택합니다.
Malformed boundary 값은 LlmPlatformBoundaryError입니다.
Provider 연결 recipe¶
OpenRouter와 표준 OpenAI-compatible API¶
OpenRouter는 별도 dialect로 추론하지 않습니다. OpenAI Chat Completions API와
OpenAICompatibleDialect.STANDARD를 사용하고 endpoint와 API key를 profile에
명시합니다. OpenAI 공식 endpoint와 OpenRouter를 포함한 standard dialect는 API key가
필수이며 ambient key로 fallback하지 않습니다.
from os import environ
from pydantic import SecretStr
from spakky.plugins.llm.config import LlmProfile, LlmProviderApi
model_gateway = LlmProfile(
provider="openrouter",
api=LlmProviderApi.OPENAI_CHAT_COMPLETIONS,
base_url=environ["OPENROUTER_BASE_URL"],
api_key=SecretStr(environ["OPENROUTER_API_KEY"]),
)
Physical model ID에 /가 들어가도 그대로 route에 보존됩니다.
from spakky.plugins.llm.config import LlmModelRoute
coding_route = LlmModelRoute(
profile="model_gateway",
model="moonshotai/kimi-k2",
)
로컬 vLLM dialect¶
vLLM은 같은 OpenAI Chat Completions adapter를 사용하지만 extension을 명시적으로
활성화합니다. chat_template_kwargs는 connection profile이 아니라 model route에 둡니다.
from spakky.plugins.llm.config import (
LlmModelRoute,
LlmProfile,
LlmProviderApi,
OpenAICompatibleDialect,
)
local_inference = LlmProfile(
provider="vllm",
api=LlmProviderApi.OPENAI_CHAT_COMPLETIONS,
base_url="http://127.0.0.1:8000/v1",
openai_dialect=OpenAICompatibleDialect.VLLM,
)
local_route = LlmModelRoute(
profile="local_inference",
model="Qwen/Qwen3-8B",
chat_template_kwargs={"enable_thinking": False},
)
vLLM dialect는 명시 base_url이 필요하며 API key는 선택 사항입니다. Key를 생략한
vLLM에만 SDK 호출용 non-secret not-required sentinel을 사용하고, 기본 local profile은
EMPTY를 명시합니다. Standard dialect에 chat_template_kwargs나 extra_body를 붙이면 설정 오류로
중단됩니다.
vLLM route가 supports_reasoning=True이면 assistant history의 metadata["reasoning"](문자열)을 요청의 assistant message reasoning_content로 다시 싣습니다. 응답이 reasoning을 돌려주는 key와 같으므로 응답 metadata를 history에 그대로 옮기면 reasoning_content를 읽는 chat template이 지난 reasoning을 다시 렌더링할 수 있습니다. 값이 문자열이 아니면 LlmResponseError입니다. Standard dialect나 supports_reasoning=False route에서는 싣지 않습니다.
요청마다 route 덮기 (LlmRequestOptions)¶
LlmRequestOptions는 AgentRunExtension입니다. ModelRequest.extensions에 실으면
LlmAgentModel이 model_selection으로 고른 route 하나를 그 요청에서만 덮습니다.
| Field | 기본값 | 덮는 방식 |
|---|---|---|
model |
None |
route.model 대신 쓸 provider model 이름; None이면 route 값 |
chat_template_kwargs |
{} |
route.chat_template_kwargs 위에 key마다 덮음 (vLLM dialect 전용) |
reasoning_effort |
None |
route.reasoning_effort 대신 씀; None이면 route 값 (openai-responses 전용) |
extra_body |
{} |
route.extra_body 위에 key마다 덮음 (vLLM dialect 전용) |
from spakky.agent import ModelMessage, ModelMessageRole, ModelRequest, ModelSelection
from spakky.plugins.llm.provider import LlmRequestOptions
request = ModelRequest(
messages=(ModelMessage(ModelMessageRole.USER, "요약해 줘"),),
model_selection=ModelSelection("local/chat"),
extensions=(
LlmRequestOptions(
chat_template_kwargs={"enable_thinking": False},
extra_body={"add_generation_prompt": False, "continue_final_message": True},
),
),
)
- 덮은 값은
LlmModelRoute검증을 다시 거칩니다(modelstrip, 문자열"true"/"false"의 boolean 정규화). 그래서 adapter, response cache key, 응답의 routing metadata가 모두 덮은 route를 봅니다. Capability는 원래 route의 것을 그대로 씁니다. fallbacks의 route는 다른 model이므로 덮지 않습니다.- vLLM dialect가 아닌 profile에
chat_template_kwargs나extra_body를,openai-responses가 아닌 profile에reasoning_effort를 주면LlmUnsupportedFeatureError입니다. Streaming에서는llm_feature_unsupportederror event로 끝납니다. - 빈
model,extra_body의 framework 예약 key(chat_template_kwargs,structured_outputs), template scalar나 JSON이 아닌 값은LlmRequestOptionsError(LlmModelSelectionError의 하위 타입)입니다. - 요청마다 다른 token은 이 extension이 아니라
credential_provider로 줍니다.LlmCredentialRequest.request에 요청 전체가 들어오므로 application은 자기AgentRunExtension을 거기서 읽어 token을 고를 수 있습니다.
LlmRequestOptions를 싣지 않은 요청은 이전과 같습니다.
OpenAI Responses API와 ChatGPT 로그인¶
LlmProviderApi.OPENAI_RESPONSES는 Responses API를 SSE streaming으로 호출합니다. API key로 쓰는
경우 standard dialect와 api_key를, Sign in with ChatGPT token으로 쓰는 경우 chatgpt
dialect와 credential_provider를 지정합니다.
from spakky.plugins.llm.config import (
LlmModelRoute,
LlmProfile,
LlmProviderApi,
LlmReasoningEffort,
OpenAICompatibleDialect,
)
chatgpt_profile = LlmProfile(
provider="openai",
api=LlmProviderApi.OPENAI_RESPONSES,
openai_dialect=OpenAICompatibleDialect.CHATGPT,
credential_provider="chatgpt",
)
builder_route = LlmModelRoute(
profile="chatgpt",
model="gpt-6.1-sol",
reasoning_effort=LlmReasoningEffort.LOW,
)
model은 로그인한 계정에서 쓸 수 있는 model ID를 넣습니다. chatgpt dialect는 system
message를 developer role로 보내고 temperature와 top_p를 제거합니다. 이 두 parameter는
ChatGPT 로그인 경로에서 HTTP 400으로 거절됩니다.
Token을 호출 시점에 공급하기¶
credential_provider는 application이 등록한 ILLMCredentialProvider Pod의 name입니다.
Adapter는 attempt마다 access_token()을 호출하고, provider가 HTTP 401로 거절하면 거절된
token을 rejected_token에 담아 한 번 더 호출한 뒤 요청을 정확히 한 번 재시도합니다.
openai-chat-completions profile에서도 같은 방식으로 쓸 수 있습니다.
Token을 얻고 갱신하는 일은 spakky-oauth에 맡길 수 있습니다. 두 플러그인은
서로 import하지 않으므로 연결하는 Pod는 application이 씁니다.
from typing import override
from spakky.core.pod.annotations.pod import Pod
from spakky.plugins.llm.provider import (
ILLMCredentialProvider,
LlmAccessToken,
LlmCredentialRequest,
)
from spakky.plugins.oauth.client import OAuthClient
@Pod()
class ChatGptCredentialProvider(ILLMCredentialProvider):
def __init__(self, oauth: OAuthClient) -> None:
self._oauth = oauth
@property
@override
def name(self) -> str:
return "chatgpt"
@override
async def access_token(self, credential: LlmCredentialRequest) -> LlmAccessToken:
user_id = str(credential.request.metadata["user_id"])
return await self._oauth.access_token(
"chatgpt",
user_id,
rejected_token=credential.rejected_token,
)
이 예시는 호출자가 ModelRequest.metadata["user_id"]에 주체를 넣는다고 가정합니다. 주체를
어디서 읽을지는 application이 정합니다. 사용자가 아직 로그인하지 않았으면
OAuthAuthorizationRequiredError가 그대로 호출자에게 전달됩니다.
실패 구분¶
| 상황 | 에러 | 재시도 |
|---|---|---|
| 계정·플랜 사용량 소진 | LlmQuotaExhaustedError |
불가. 사용자에게 안내 |
| 일시적 요청 속도 제한 | LlmRateLimitError |
가능 |
| Terminal event 없이 stream 종료 | LlmStreamInterruptedError |
가능 |
| Credential 거절(한 번 갱신 뒤에도) | LlmAuthenticationError |
불가. 다시 로그인 |
Dialect별 요청 차이 전체, 실제 계정으로 확인한 범위와 확인하지 못한 범위는 API reference를 봅니다.
Anthropic Messages¶
Anthropic은 OpenAI compatibility layer가 아니라 native Messages adapter를 사용합니다.
base_url을 생략하면 adapter가 공식 endpoint를 명시하고 API key는 반드시 profile에서
제공합니다.
from os import environ
from pydantic import SecretStr
from spakky.plugins.llm.config import LlmProfile, LlmProviderApi
native_messages = LlmProfile(
provider="anthropic",
api=LlmProviderApi.ANTHROPIC_MESSAGES,
api_key=SecretStr(environ["ANTHROPIC_API_KEY"]),
)
Gemini Developer API¶
Gemini Developer API는 Google의 공식 제품명입니다. 여기서 “Developer”는 개발용 환경이나 비상용 endpoint라는 뜻이 아닙니다. 제품 선택이 Gemini Developer API라면 운영 서비스도 배포 runtime secret으로 API key를 공급할 수 있습니다.
from os import environ
from pydantic import SecretStr
from spakky.plugins.llm.config import (
GoogleCredentialStrategy,
LlmProfile,
LlmProviderApi,
)
public_google_api = LlmProfile(
provider="google",
api=LlmProviderApi.GOOGLE_GEMINI_DEVELOPER,
api_key=SecretStr(environ["GEMINI_API_KEY"]),
google_credential_strategy=GoogleCredentialStrategy.API_KEY,
)
이 backend는 API-key 전략을 명시해야 하며 Vertex project, location, service-account
file을 함께 받을 수 없습니다. Adapter는 설치된 google-genai의 Developer API mode를
명시하고 https://generativelanguage.googleapis.com/ endpoint를 사용합니다.
GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_ENTERPRISE, ambient project/location은
이 profile을 Vertex AI로 바꾸지 못합니다.
Vertex AI¶
Vertex AI는 별도의 google-vertex API family입니다. 설치된 google-genai 2.19.0에서는
adapter가 enterprise mode를 명시하고, profile의 project와 location 및 선택한 credential을
client에 전달합니다. 이 “enterprise mode”도 배포 환경 이름이 아니라 SDK backend mode입니다.
base_url을 생략해도 endpoint 선택을 SDK ambient 환경에 맡기지 않습니다. Location이
global이면 https://aiplatform.googleapis.com/, multi-region us와 eu이면 각각
https://aiplatform.us.rep.googleapis.com/와 https://aiplatform.eu.rep.googleapis.com/,
그 밖의 endpoint-safe lowercase region이면
https://{location}-aiplatform.googleapis.com/을 adapter가 명시합니다.
Operator가 LlmProfile.base_url을 직접 설정한 경우에만 이 official endpoint를
override합니다. GOOGLE_VERTEX_BASE_URL, GOOGLE_GEMINI_BASE_URL, ambient location은
endpoint를 바꾸지 못합니다.
ADC (Application Default Credentials)를 의도적으로 선택하는 경우:
from spakky.plugins.llm.config import (
GoogleCredentialStrategy,
LlmProfile,
LlmProviderApi,
)
cloud_enterprise = LlmProfile(
provider="google",
api=LlmProviderApi.GOOGLE_VERTEX,
google_credential_strategy=GoogleCredentialStrategy.ADC,
google_project="my-google-cloud-project",
google_location="us-central1",
)
명시적인 service-account 파일을 mount하는 경우:
from pathlib import Path
from spakky.plugins.llm.config import (
GoogleCredentialStrategy,
LlmProfile,
LlmProviderApi,
)
cloud_enterprise = LlmProfile(
provider="google",
api=LlmProviderApi.GOOGLE_VERTEX,
google_credential_strategy=GoogleCredentialStrategy.SERVICE_ACCOUNT_FILE,
google_project="my-google-cloud-project",
google_location="europe-west4",
google_service_account_file=Path("/var/run/secrets/google/service-account.json"),
)
ADC를 선택하면 credential 자체만 google.auth.default()에서 찾습니다. Project와 location은
ambient 값이나 ADC가 발견한 project로 추론하지 않고 profile 값을 사용합니다.
Service-account 전략은 지정한 파일만 읽으며 ADC로 fallback하지 않습니다. API key와 Vertex
credential 전략을 섞거나 project/location을 생략하면 설정 단계에서 실패합니다.
Profile 이름은 접근 제어가 아닙니다. 개발자 workstation의 ADC identity가 설정한 project에
IAM 권한을 가지고 있다면 그 workstation에서도 Vertex에 접근할 수 있습니다. 특정 배포
identity와 파일에 고정해야 한다면 SERVICE_ACCOUNT_FILE 전략을 선택하고, 실제 권한은
Google IAM에서 제한하세요. -prod 같은 profile suffix는 프레임워크가 해석하지 않습니다.
로컬·CI에서 이 routing을 검증하는 데 상용 계정은 필요하지 않습니다. Repository의 acceptance test는 공식 SDK client 경계를 deterministic fake로 대체해 model ref, project, location, credential 선택과 routing metadata를 network 없이 검증합니다. 실제 backend를 호출하는 acceptance 환경에서만 해당 배포 credential을 준비하세요.
Route capability 선언¶
Capability는 provider 이름에서 추론하지 않고 각 LlmModelRoute가 선언합니다.
| 필드 | 기본값 | 의미 |
|---|---|---|
supports_reasoning |
false |
reasoning channel 지원 |
context_window_tokens |
None |
알려진 context window; 값이 있으면 양수 |
supports_token_counting |
false |
호출 전 token counting 지원 |
input_modalities / output_modalities |
text only | 비어 있을 수 없는 portable modality 집합 |
supports_tools |
false |
tool calling 지원 |
supports_structured_output |
false |
structured output 지원 |
from spakky.agent import ModelCapability, ModelModality
from spakky.plugins.llm.config import LlmModelRoute
support_route = LlmModelRoute(
profile="cloud_enterprise",
model="publishers/google/models/gemini-2.5-pro",
capability=ModelCapability(
supports_reasoning=True,
context_window_tokens=1_000_000,
supports_token_counting=True,
input_modalities=frozenset({ModelModality.TEXT, ModelModality.IMAGE}),
output_modalities=frozenset({ModelModality.TEXT}),
supports_tools=True,
supports_structured_output=True,
),
)
LlmAgentModel.capability은 default route의 선언을 반환하고,
capability_for(ModelSelection(...))은 선택한 route의 정확한 선언을 반환합니다. 이 값은
자동 탐지 결과가 아니므로 operator가 실제 모델과 endpoint의 지원 범위에 맞게 유지해야
합니다. Capability descriptor만으로 provider payload encoding이 생기지는 않습니다.
Core의 portable input은 TextPart, ImagePart, AudioPart, VideoPart, DocumentPart이며,
선택 route의 input_modalities와 실제 provider adapter가 요청 part를 모두 지원해야 합니다.
가장 작은 attachment 예제와 URI 검증 경계는 앞의
Multimodal route와 URI authority를 확인하세요.
실행에서 모델 선택하기¶
Python caller가 아는 선택 API는 model_ref 하나뿐입니다.
from spakky.agent import ModelSelection, RunAgentInput
run_input = RunAgentInput(
state_id="run-42",
instruction="고객 문의를 분류해 주세요.",
model_selection=ModelSelection(model_ref="support/primary"),
)
활성 model이 LlmAgentModel인 경로에서 선택을 생략하면 그 router의 default_model이
사용됩니다. 다른 IAgentModel 구현에는 자체 selection/default 정책이 있을 수 있습니다.
Logical ref와 profile key는 앞뒤 공백만 제거하는 case-sensitive opaque key입니다. /는
namespace 문법으로 해석되지 않으며, physical model ID의 /도 그대로 provider SDK에
전달됩니다. 예를 들어 support/Primary와 support/primary는 서로 다른 ref입니다.
Blank ref는 core 계약에서 실패합니다. Direct complete()에서 catalog에 없는 ref는
LlmModelSelectionError를 raise하지만, stream()은 exception을 protocol shape parser로
보내지 않고 llm_model_selection_invalid ERROR 뒤 DONE으로 terminalize합니다. Caller가
provider, profile, model, selection metadata를 보내는 legacy 형태는 지원되지
않습니다. Raw model ID가 우연히 provider/model처럼 보여도 catalog entry가 없으면
추론하거나 fallback하지 않습니다.
AG-UI와 A2A wire 계약¶
Wire protocol에서도 선택 object는 modelRef 하나만 가집니다.
| Inbound | 정확한 shape |
|---|---|
| AG-UI | forwardedProps.modelSelection.modelRef |
| A2A | data part의 modelSelection.modelRef |
A2A는 canonical camelCase outer key만 허용하며 legacy model_selection을 거부합니다.
한 message의 모든 data part에서 selector는 최대 하나여야 합니다. 내부에 modelRef 외
필드를 넣거나 blank/non-string 값을 보내면 protocol adapter가 request를 거부합니다.
AG-UI도 내부 object가 정확히 modelRef 하나를 가져야 합니다. Well-formed unknown ref는
두 protocol 모두 shape parser를 통과한 뒤 앞 절의 terminal model error로 표면화됩니다.
진단과 교체 가능성¶
성공 response와 terminal stream event는 최소한 다음 routing metadata를 보존합니다.
{
"model_ref": "support/primary",
"profile": "cloud_enterprise",
"provider": "google",
"model": "publishers/google/models/gemini-2.5-pro"
}
Finish reason, response ID 같은 provider 응답 metadata가 추가될 수 있지만 API key,
service-account 내용, headers, endpoint는 routing evidence에 포함되지 않습니다. Unknown
ref는 선택되지 않은 default route를 꾸며 내지 않고 요청한 model_ref만 오류 metadata에
남깁니다.
Route target 교체는 operator 설정에서 수행합니다.
support/primaryref는 유지합니다.- 새 connection profile을 추가합니다.
- 해당 ref의
LlmModelRoute.profile과model을 새 target으로 바꿉니다. - Route capability를 새 모델에 맞게 갱신합니다.
- 시작 단계 검증과 response/event의 exact routing metadata를 확인합니다.
Provider adapter 구현 교체¶
공식 OpenAI, Anthropic, Google adapter는 ILLMProvider.is_default=True인 replaceable
default입니다. Application이 기여한 ILLMProvider는 이 property를 override하지 않으면
non-default custom implementation입니다. Registry는 API family별로 다음 규칙을
LlmAgentModel 생성, 일반적으로 app.start() 시점에 적용합니다.
- Custom implementation이 정확히 하나면 같은 API의 first-party default를 교체합니다.
- Custom implementation이 둘 이상이면 임의 선택하지 않고
LlmConfigurationError입니다. - Custom이 없으면 default가 정확히 하나여야 합니다. Default가 없거나 둘 이상이어도
LlmConfigurationError입니다. - Configured profile이 요구하는 API가 최종 registry에 없으면 bootstrap에 실패합니다.
이 경로는 logical route의 target만 바꾸는 것과 다릅니다. 같은 API family의 SDK adapter 구현 자체를 application Pod로 대체해야 할 때 사용합니다.
검증 시점도 구분하세요.
LlmConfig/profile/route field와 catalog reference는 객체 생성 시 Pydantic이 검증합니다.- API family별 default/custom provider ambiguity와 configured API 누락은
LlmAgentModel생성, 일반적으로app.start()에서LlmConfigurationError입니다. - Standard OpenAI/Anthropic API key, vLLM explicit base URL, Google credential load와 실제
endpoint client 구성은 선택 route가
complete()/stream()을 시작할 때LlmConfigurationError로 검증됩니다.
이 흐름은 Spring Boot의 auto-configuration과 같은 방향을 따릅니다. 바로 실행 가능한 local 기본값을 제공하되, 운영 설정은 typed configuration으로 완전히 교체할 수 있고, 잘못된 override는 조용히 다른 provider로 흘리지 않으며, 실제 선택 결과는 진단 가능한 metadata로 남깁니다.
함께 보기¶
- AI Agent 개발:
IAgentModel을 Agent에 주입하는 기본 흐름 - AI Agent 심화:
RunAgentInput과 model capability 사용 경계 - AG-UI 어댑터: AG-UI request 전체 shape
- A2A 어댑터: A2A data part 전체 shape
- spakky-llm API Reference: 모든 설정 필드와 provider 오류 계약
- 에러 계층 구조: stable stream error code