Messages API
Anthropic SDK를 그대로 쓰기
POST https://alprouter.alpsoft.io/api/v1/messages는 Anthropic의 Messages API와 같은 모양이에요. Claude 모델에는 요청을 다른 형식으로 바꾸지 않고 전해서, 생각 블록과 캐시 표시, 도구 호출이 그대로 오가요.
시작하기
Anthropic SDK의 기본 주소를 https://alprouter.alpsoft.io/api로 바꿔요. SDK가 뒤에 /v1/messages를 붙여요.
import anthropic
client = anthropic.Anthropic(
base_url="https://alprouter.alpsoft.io/api",
api_key="alp-내-키",
)
with client.messages.stream(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "안녕하세요"}],
) as stream:
for text in stream.text_stream:
print(text, end="")모델 이름
Anthropic에서 쓰는 이름과 AlpRouter의 이름을 모두 받아요. 예를 들어 claude-sonnet-5-5와 anthropic/claude-sonnet-5.5는 같은 모델이에요.
| Anthropic 이름 | AlpRouter 이름 |
|---|---|
claude-fable-5-2 | anthropic/claude-fable-5.2 |
claude-opus-5-5 | anthropic/claude-opus-5.5 |
claude-sonnet-5-5 | anthropic/claude-sonnet-5.5 |
claude-haiku-5-5 | anthropic/claude-haiku-5.5 |
claude-opus-5 | anthropic/claude-opus-5 |
claude-sonnet-5 | anthropic/claude-sonnet-5 |
claude-opus-4-8 | anthropic/claude-opus-4.8 |
claude-opus-4-7 | anthropic/claude-opus-4.7 |
claude-opus-4-6 | anthropic/claude-opus-4.6 |
claude-opus-4-5 | anthropic/claude-opus-4.5 |
claude-sonnet-4-6 | anthropic/claude-sonnet-4.6 |
claude-haiku-4-5 | anthropic/claude-haiku-4.5 |
이 주소는 Claude 모델만 받아요. 다른 모델은 /chat/completions로 보내 주세요.
받는 항목
| 구분 | 항목 |
|---|---|
| 그대로 전해요 | messages, system, max_tokens, tools, tool_choice, thinking, output_config, stop_sequences, temperature, top_p, top_k, metadata |
| 블록 | text, image, document(PDF), tool_use, tool_result, thinking과 cache_control 표시 |
| 받지 않아요 | 웹 검색처럼 서버가 대신 실행하는 도구(web_search, web_fetch, code_execution). 400 오류로 알려 드려요 |
| 빼고 보내요 | service_tier, speed처럼 요금이 달라지는 항목과, 공급사가 아직 받지 않는 새 항목과 anthropic-beta 값 |
빼고 보낸 항목이 있으면 응답 헤더 x-alprouter-dropped에 이름을 적어 드려요. 헤더가 없으면 보낸 그대로 전해진 거예요.
사용량과 요금
- 응답의
usage는 Anthropic과 같은 모양이에요. 요금은 모델 화면의 가격으로 계산해요. - 캐시에 쓴 토큰은 5분짜리는 모델의 캐시 쓰기 가격, 1시간짜리는 입력 가격의 2배로 계산해요.
- 응답 헤더
request-id의 값으로GET https://alprouter.alpsoft.io/api/v1/generation?id=에서 요금을 볼 수 있어요.
토큰 수 세기
POST https://alprouter.alpsoft.io/api/v1/messages/count_tokens는 보내기 전에 입력 토큰 수를 알려 줘요. 요금은 받지 않아요. 공급사가 세어 주는 모델은 정확한 값을, 그렇지 않은 모델은 실제보다 조금 넉넉하게 어림한 값을 돌려줘요.
제한
- 글은 400KB까지, 이미지는 20장까지, 문서는 5개까지 보낼 수 있어요.
- 오류는 Anthropic과 같은 모양(
type: error)으로 돌려줘요. - 받는 도중에 연결을 끊어도 Claude 모델은 끝까지 만든 만큼 요금이 나가요. 공급사(Amazon Bedrock)가 연결이 끊겨도 만들기를 멈추지 않기 때문이에요. 길이를 정하고 싶다면 max_tokens를 적어 주세요.