ํ๋ก์ ํธ์ API ์์ด AI ๋ถ์ด๊ธฐ
ํ๋ก์ ํธ์ ์์ฝ, ๋ถ๋ฅ, ๋ฒ์ญ ๊ฐ์ AI ๊ธฐ๋ฅ์ ๋ฃ๊ณ ์ถ๋ค. ๊ทธ๋ฐ๋ฐ API ํธ์ถ์ ์ฌ์ฉํ์ฌ ์คํ ๋จ๊ณ์์ ์ด๊ฒ์ ๊ฒ ๋๋ ค๋ณด๋ค ๋ณด๋ฉด ํ ๋ฌ์ ์์ญ ๋ฌ๋ฌ๊ฐ ์ฐ์ต๊ฒ ๋๊ฐ๋ค. ์ด๋ฏธ ๊ตฌ๋ ์ ์ฐ๊ณ ์๋ค๋ฉด, API ํธ์ถ ๋์ Claude Code CLI๋ฅผ subprocess๋ก ๋ถ๋ฌ์ ๊ฐ์ ์ผ์ ํ ์ ์๋ค. Anthropic์ด ๊ณต์์ ์ผ๋ก ์ง์ํ๋ ํค๋๋ฆฌ์ค ๋ชจ๋๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด ๋ฐฉ์์ด ๋ง๋ ์ํฉ์ธ์ง ๋จผ์ ํ์ธํด ๋ณด์.
| ์ํฉ | ์ ํฉ๋ |
| ํผ์ ์ฐ๋ ์ฌ์ด๋ ํ๋ก์ ํธ, ๋ก์ปฌ ์๋ํ | ๋งค์ฐ ์ ํฉ |
| ํ๋กฌํํธ ์คํ, ๋ฐ๋ณต ํ ์คํธ | ์ ํฉ |
| ๋ฐฐ์น ์ฒ๋ฆฌ (ํ๋ฃจ ์์ญ~์๋ฐฑ ๊ฑด) | ๊ตฌ๋ ๋ฆฌ๋ฐ ์ฒดํฌ ํ์ |
| ์ธ๋ถ ์ฌ์ฉ์์๊ฒ ์๋น์ค ์ ๊ณต | API๋ก ๊ฐ์ผ ํจ |
| 24/7 ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋ํ | ๋ ์ดํธ ๋ฆฌ๋ฐ ์์ฃผ ๊ฑธ๋ฆผ |
API ์ข ๋์ ๋ ๊ฐ์ธ ํ๋ก์ ํธ์ ์ต์ ํ๋ผ ์์ง ์๋ค. Anthropic API ์๊ธ์ ๋ณธ๋ B2B ์๋น์ค ์ํฌ๋ก๋ ๊ธฐ์ค์ผ๋ก ์ค๊ณ๋๋ค. ์ซ์๋ง ๋ณด๋ฉด ์ ๋ ดํด ๋ณด์ด์ง๋ง, ๊ฐ์ธ ํ๋ก์ ํธ ๋งฅ๋ฝ์ ์ฌ๋ ค๋์ผ๋ฉด ์ด์ผ๊ธฐ๊ฐ ๋ฌ๋ผ์ง๋ค. ์๋ฅผ ๋ค์ด ์ฌ์ด๋ ํ๋ก์ ํธ์ ๋ฌธ์ ์์ฝ ๊ธฐ๋ฅ ํ๋๋ฅผ ๋ถ์๋ค๊ณ ํด๋ณด์. ๋ฌธ์๋น ํ๊ท 3์ฒ ํ ํฐ ์ ๋ ฅ / 500 ํ ํฐ ์ถ๋ ฅ์ด๊ณ , ๊ฐ๋ฐ ์ค์ ํ๋กฌํํธ ํ๋ํ๋ฉด์ ํ๋ฃจ 100๋ฒ์ฏค ์คํ์ ์ผ๋ก ๋๋ฆฐ๋ค๋ฉด Opus ๊ธฐ์ค ๊ณ์ฐ์ ์ด๋ ๋ค.
- ์ ๋ ฅ: 3000 × 100 = 30๋ง ํ ํฐ → $1.5 / ์ผ
- ์ถ๋ ฅ: 500 × 100 = 5๋ง ํ ํฐ → $1.25 / ์ผ
- ํ๋ฃจ ์ฝ $2.75, ํ ๋ฌ ์ฝ $82
๋ฐ๋ฉด Pro ๊ตฌ๋ ์ ์ $20 ์ ์ก์ด๋ค.
๋ค๋ฅธ ๋์์ ๋ชจ๋ ์ด๋๊ฐ ๋ถ์กฑํ๋ค.
| ๋์ | ํ๊ณ |
| API ์ง์ ํธ์ถ | ๋น์ฉ ์์ธก ๋ถ๊ฐ, ์ฒญ๊ตฌ์ ๊ณตํฌ |
| ๋ฌด๋ฃ ํฐ์ด(Free) | ์ผ์ผ ์ ํ, ์๋ํ ์ค์ง ๋ถ๊ฐ |
| ๋ก์ปฌ LLM (Ollama ๋ฑ) | Claude ๋๋น ์ฑ๋ฅ ๊ฒฉ์ฐจ, ์ธํ /์ ์ง ๋ถ๋ด |
| Claude.ai์์ ์๋ ๋ณต๋ถ | ์๋ํ ๋ถ๊ฐ, ํ์ดํ๋ผ์ธ ๋ชป ๋ง๋ฆ |
1. Claude Code CLI subprocess ํธ์ถ
Claude Code๋ ์๋ ํฐ๋ฏธ๋์์ ๋ํํ์ผ๋ก ์ฐ๋ ์ฝ๋ฉ CLI์ง๋ง, -p(๋๋ --print) ํ๋๊ทธ๋ฅผ ๋ถ์ด๋ฉด ๋น๋ํํ(non-interactive) ๋ชจ๋๋ก ๋์๊ฐ๋ค. ์ด ๋ชจ๋๋ ๊ณผ๊ฑฐ์ headless mode๋ผ๊ณ ๋ถ๋ ธ๊ณ , ๊ณต์ ๋ฌธ์์๋ CI/CD, ์คํฌ๋ฆฝํธ, ์๋ํ ์ฉ๋๋ก ์ฐ๋ผ๊ณ ์๋ด๋ผ ์๋ค.
- Claude Code๋ ๊ตฌ๋
๊ณ์ ์ผ๋ก ์ธ์ฆ๋๋ค.
claude loginํ ๋ฒ์ด๋ฉด Pro/Max ๊ตฌ๋ ์ผ๋ก ์ฐ๊ฒฐ๋๋ค. -p๋ชจ๋์์๋ ๊ทธ ์ธ์ฆ์ด ๊ทธ๋๋ก ์ฐ์ธ๋ค. ์ฆ, API ํค ์์ด subprocess ํธ์ถ๋ก ๋ชจ๋ธ์ ์ธ ์ ์๋ค.
ํ๊ฒฝ ๋ณ์์ ANTHROPIC_API_KEY๊ฐ ์ค์ ๋ผ ์์ผ๋ฉด ๊ตฌ๋ ์ด ์๋๋ผ API๋ก ๊ฒฐ์ ๋๋ค. subprocess ํธ์ถ ์ ์ ๋ฐ๋์ ์ด ๋ณ์๋ฅผ ๋น์๋ฌ์ผ ํ๋ค.
unset ANTHROPIC_API_KEY
claude login # ๊ตฌ๋
๊ณ์ ์ผ๋ก ์ธ์ฆ
claude -p "Say hello in 15 words"
2. Claude Code -p ๊ธฐ๋ณธ๊ธฐ
๊ฐ์ฅ ๋จ์ํ ํธ์ถ
claude -p "2 + 2๋?"
ํ๋กฌํํธ๋ฅผ ์ธ์๋ก ๋๊ธฐ๋ฉด, Claude๊ฐ ๋ต์ stdout์ผ๋ก ๋ฑ๊ณ ์ข ๋ฃ๋๋ค. ํ์ดํ๋ก stdin์ ๋ฐ์ ์๋ ์๋ค.
cat report.txt | claude -p "์ด ๋ฌธ์๋ฅผ ์ธ ์ค๋ก ์์ฝํด์ค"
์์ฃผ ์ฐ๋ ํ๋๊ทธ
| ํ๋๊ทธ | ์ญํ |
| -p, --print | ๋น๋ํํ ์คํ |
| --output-format json | ๊ตฌ์กฐํ๋ JSON ๊ฒฐ๊ณผ |
| --output-format stream-json | ์คํธ๋ฆฌ๋ฐ JSON |
| --allowedTools "Read,Edit" | Claude๊ฐ ์ธ ์ ์๋ ๋๊ตฌ๋ฅผ ์ ํ |
| --append-system-prompt | ์์คํ ํ๋กฌํํธ์ ์ง์ ์ถ๊ฐ |
| --system-prompt | ์์คํ ํ๋กฌํํธ ์ ์ฒด ๊ต์ฒด |
| --continue | ์ง์ ๋ํ ์ด์ด๊ฐ๊ธฐ |
| --resume <session_id> | ํน์ ์ธ์ ์ด์ด๊ฐ๊ธฐ |
| --bare | ์๋ ํ์ ์คํตํด์ ์์ ์๊ฐ ๋จ์ถ |
๊ธฐ๋ณธ ํธ์ถ์ ์์ํ ๋๋ง๋ค hooks, skills, plugins, MCP ์๋ฒ, CLAUDE.md ๋ฑ์ ์๋ ํ์ํ๋ค. subprocess๋ก ๋๋ ํธ์ถํ ๋๋ ์ด ์ค๋ฒํค๋๊ฐ ๋์ ๋๋ค. ์๋ํ ์ฉ๋๋ผ๋ฉด --bare๋ฅผ ๋ถ์ด์.
3. ๊ตฌ์กฐํ๋ ์ถ๋ ฅ ๋ฐ์๋ด๊ธฐ
subprocess ๊ฒฐ๊ณผ๋ฅผ ํ๋ก๊ทธ๋จ์ด ํ์ฑํ๋ ค๋ฉด JSON์ด ๋ต์ด๋ค.
--output-format json
claude -p "ํจ์ ์ด๋ฆ ๋ฆฌ์คํธ๋ฅผ ๋ฝ์์ค" --output-format json
๊ฒฐ๊ณผ๋ ์ด๋ฐ ํํ๋ก result ํ๋๋ง ํ์ฑํ๋ฉด ๋๋ค.
{
"type": "result",
"subtype": "success",
"result": "์ค์ ์๋ต ํ
์คํธ...",
"duration_ms": 1234,
"total_cost_usd": 0.01,
"session_id": "...",
"structured_output": null
}
--json-schema๋ก ์คํค๋ง ๊ฐ์
์ถ๋ ฅ์ ํน์ ๊ตฌ์กฐ์ ๋ง์ถ๊ณ ์ถ์ผ๋ฉด JSON Schema๋ฅผ ๋ช ์ํ ์ ์๋ค. ์ด๋ฌ๋ฉด Claude๊ฐ ์คํค๋ง์ ๋ง์ถฐ ์ถ๋ ฅํ๋ค. ํ์ฒ๋ฆฌ์์ ์ ๊ท์ ํ์ฑํ๋ ์ง์ฅ์ ํผํ ์ ์๋ค. ๋จ, --json-schema๋ฅผ ์ฐ๋ฉด ํ์ฑ๋ ๊ฒฐ๊ณผ๋ result๊ฐ ์๋๋ผ structured_output ํ๋์ dict ํํ๋ก ๋ค์ด์จ๋ค.
claude -p "์ด ํ์ผ์์ ํจ์ ์ด๋ฆ๋ง ๋ฝ์์ค" \
--output-format json \
--json-schema '{
"type": "object",
"properties": {
"functions": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["functions"]
}' < auth.py
๋ํ ์ด์ด๊ฐ๊ธฐ
์์ฝ ํ ๋ฒ → ํ์ ์ง๋ฌธ ๊ฐ์ ํ๋ฆ์ด๋ผ๋ฉด --continue๋ฅผ ์ฐ๋ฉด ๋๋ค. --continue๋ "์ง์ ์ธ์
"์ ์ฐพ๊ธฐ ๋๋ฌธ์ ๋ณ๋ ฌ ์คํ์๋ ์ ํฉํ์ง ์๋ค. ์ฌ๋ฌ ํ๋ฆ์ ๋์์ ๋๋ ค์ผ ํ๋ฉด --resume <session_id>๋ก ์ธ์
ID๋ฅผ ๋ช
์์ ์ผ๋ก ๊ด๋ฆฌํ์.
claude -p "์ด ์ฝ๋๋ฒ ์ด์ค์ ์ฑ๋ฅ ์ด์๋ฅผ ๋ถ์ํด์ค"
claude -p "๋ฐฉ๊ธ ์ฐพ์ ์ด์ ์ค DB ๊ด๋ จ๋ง ์ถ๋ ค์ค" --continue
claude -p "๊ฐ ์ด์์ ๋ํ ์์ ์์ JSON์ผ๋ก ์ ๋ฆฌ" --continue --output-format json
4. Python subprocess๋ก ๊ฐ์ธ๊ธฐ
Python์์ Claude Code CLI๋ฅผ subprocess๋ก ๋ถ๋ฌ ์ฐ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ ํํ์ด๋ค.
import subprocess
import json
def ask_claude(prompt: str) -> dict:
result = subprocess.run(
["claude", "-p", "--bare", "--output-format", "json", prompt],
capture_output=True,
text=True,
timeout=60,
)
data = json.loads(result.stdout)
if data.get("subtype") != "success":
raise RuntimeError(f"claude error: {data}")
return data
res = ask_claude("ํ์ด์ฌ์์ ๋ฆฌ์คํธ ์ ๋ ฌํ๋ ํ ์ค์ง๋ฆฌ ์ฝ๋")
print(res["result"])
print(f"์์ ์๊ฐ: {res['duration_ms']}ms")
๊ธด ์ ๋ ฅ์ stdin์ผ๋ก
ํ๋กฌํํธ๊ฐ ๊ธธ๊ฑฐ๋ ๋์ ์ฝํ ์ธ ๊ฐ ๋ง์ผ๋ฉด ์ธ์ ๊ธธ์ด ์ ํ์ ๊ฑธ๋ฆฐ๋ค. ์ด๋ด ๋ stdin์ผ๋ก ํ์ดํํ์.
def summarize_file(path: str) -> str:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
result = subprocess.run(
["claude", "-p", "--bare", "์ด ๋ฌธ์๋ฅผ ์ธ ์ค๋ก ์์ฝํด์ค"],
input=content,
capture_output=True,
text=True,
timeout=120,
)
return result.stdout.strip()
5. ์ค์ ํ์ฉ ์์
์ด๋ฉ์ผ ๋ถ๋ฅ๊ธฐ
๋ฐ์ ๋ฉ์ผํจ์ ํ์ด์ ์นดํ ๊ณ ๋ฆฌ์ ๊ธด๊ธ๋๋ฅผ ํ๊น ํ๋ค. ์คํค๋ง๋ฅผ ๊ฐ์ ํ๋ฉด ํ์ฑ์ด ๊น๋ํด์ง๋ค.
import json
import subprocess
SCHEMA = {
"type": "object",
"properties": {
"category": {
"type": "string",
"enum": ["์
๋ฌด", "๊ด๊ณ ", "๋ด์ค๋ ํฐ", "๊ฐ์ธ", "๊ธฐํ"]
},
"urgency": {"type": "string", "enum": ["high", "mid", "low"]},
"summary": {"type": "string"}
},
"required": ["category", "urgency", "summary"]
}
def classify_email(body: str) -> dict:
result = subprocess.run(
[
"claude", "-p", "--bare",
"--output-format", "json",
"--json-schema", json.dumps(SCHEMA),
"์ด ์ด๋ฉ์ผ์ ๋ถ๋ฅํ๊ณ ์์ฝํด:"
],
input=body,
capture_output=True,
text=True,
timeout=60,
)
data = json.loads(result.stdout)
return data["structured_output"]
Git ์ปค๋ฐ ๋ฉ์์ง ์์ฑ๊ธฐ
์คํ ์ด์ง๋ ๋ณ๊ฒฝ์ฌํญ์ ๊ธฐ๋ฐ์ผ๋ก ์ปค๋ฐ ๋ฉ์์ง๋ฅผ ๋ง๋ค์ด ์ฃผ๋ ํ ์ด๋ค.
#!/usr/bin/env bash
# .git/hooks/prepare-commit-msg
DIFF=$(git diff --cached)
[ -z "$DIFF" ] && exit 0
MSG=$(echo "$DIFF" | claude -p --bare \
--append-system-prompt "You output only a conventional commit message, no explanation." \
"Generate a conventional commit message for this diff")
echo "$MSG" > "$1"
6. ์ฃผ์์ฌํญ๊ณผ ํจ์
๋ ์ดํธ ๋ฆฌ๋ฐ
๊ตฌ๋ ์ 5์๊ฐ ๋กค๋ง ์๋์ฐ ๊ธฐ์ค์ด๋ค. subprocess๋ก ๋ฌดํ ๋ฃจํ ๋๋ ค์ ๊ฐ๊ธฐ๋ฉด ๊ธ๋ฐฉ ํ๋์ ๊ฑธ๋ฆฐ๋ค. Pro์์ ์์ฃผ ๋งํ๋ค๋ฉด ๋ฃจํ์ ์ง์ ๋ฐฑ์คํ๋ฅผ ๋ฃ๊ฑฐ๋, ๊ทธ๋๋ ์์ฃผ ๊ฑธ๋ฆฌ๋ฉด ์๋์ฐ ๋ฆฌ์ ์ ๊ธฐ๋ค๋ฆฌ๊ฑฐ๋ Max๋ก ์ฌ๋ ค์ผ ํ๋ค.
import time
for item in items:
try:
process(item)
except RuntimeError as e:
if "rate limit" in str(e).lower():
time.sleep(60)
continue
raise
ํค๋๋ฆฌ์ค ๋ชจ๋์ ํ์ ๋ ๋์
-p ๋ชจ๋๋ ๋ํํ ๋ชจ๋์ ๋ค๋ฅด๋ค. /commit ๊ฐ์ ์ฌ์ฉ์ ํธ์ถ ์ฌ๋์ ์ปค๋งจ๋๋ ๋์ํ์ง ์๋๋ค. ๊ถํ ํ๋กฌํํธ๊ฐ ๋ฐ ์ํฉ์ด ๋๋ฉด ๊ทธ๋ฅ ์คํจํ๋ค. ์ด๊ฑธ ํผํ๋ ค๋ฉด --allowedTools๋ก ํ์ํ ๋๊ตฌ๋ง ํ์ดํธ๋ฆฌ์คํธ ํด์ผ ํ๋ค. --dangerously-skip-permissions๋ผ๋ ํ๋๊ทธ๋ ์์ง๋ง, ์ด๋ฆ ๊ทธ๋๋ก ์ํํ๋ค. ๋ณธ์ธ PC์ ๊ฒฉ๋ฆฌ๋ ๋๋ ํ ๋ฆฌ๊ฐ ์๋๋ฉด ์ฐ์ง ๋ง์.
ํ๋กฌํํธ ์ธ์ ์ ๋ฆฌ์คํฌ
์ธ๋ถ ์
๋ ฅ(์น ํผ, ํฌ๋กค๋งํ HTML, ์ฌ์ฉ์ ์
๋ ฅ)์ ํ๋กฌํํธ์ ๊ทธ๋๋ก ๋ผ์ ๋ฃ๊ณ --allowedTools "Bash" ๊ฐ์ ๊ฑธ ํ์ฉํ๋ฉด ์๊ฒฉ ์ฝ๋ ์คํ ์ทจ์ฝ์ ์ด ๋๋ค. ์ธ๋ถ ์
๋ ฅ์ ์ธ ๊ฑฐ๋ฉด ๋๊ตฌ ๊ถํ์ ์ต์ํ์ผ๋ก ์ ํํ์.
๋ณตํฉ ์์ ์ ์ชผ๊ฐ๋ผ
ํ ๋ฒ์ claude -p ํธ์ถ์์ "์น ๊ฒ์ + ํ์ด์ง fetch + ๋๊ท๋ชจ ๋ฌธ์ ์์ฑ"์ ํ ๋ฒ์ ์ํค๋ฉด ์๋ต์ด ๋ฉ์ถ๋ ํ์์ด ๋ณด๊ณ ๋ ์ ์ด ์๋ค. subprocess ๋ฐฉ์์ผ๋ก ์๋ํํ ๋๋ ํ ํธ์ถ = ํ ๋จ์ ์์
์ผ๋ก ์ชผ๊ฐ๋ ๊ฒ ์์ ์ ์ด๋ค.
ToS์ ์ ์์ ์ฌ์ฉ
Claude Code ๊ตฌ๋ ์ ๋ณธ์ธ์ด ๊ฐ๋ฐํ ๋ ์ฐ๋ ๋๊ตฌ๋ค. ๊ตฌ๋ ์ธ์ฆ์ ์๋ฒ์ ๋์ ๋ค๋ฅธ ์ฌ์ฉ์์๊ฒ ์๋น์ค๋ฅผ ์ ๊ณตํ๊ฑฐ๋, 24/7 ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋ํ๋ก ์ฌ์ค์ API ํธ๋ํฝ์ ๊ตฌ๋ ์ผ๋ก ์นํํ๋ ๊ฑด ๊ตฌ๋ ์ ์ ์ ๋๋๋ค. ๋ก์ปฌ์์ ๋ณธ์ธ ํ๋ก์ ํธ์ ์ฐ๋ subprocess ํธ์ถ์ ์ ์ ์ฌ์ฉ์ ๊ฐ๊น๋ค. ์๋น์คํ ๋จ๊ณ๋ก ๊ฐ๋ฉด API๋ก ์ ํํ๋ ๊ฒ ๋ง๋ค.
์ฐธ๊ณ
https://code.claude.com/docs/en/headless
Run Claude Code programmatically - Claude Code Docs
Use the Agent SDK to run Claude Code programmatically from the CLI, Python, or TypeScript.
code.claude.com
GitHub - anthropics/claude-code: Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, a
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflo...
github.com
Plans & Pricing | Claude by Anthropic
Choose the Claude plan that fits how you solve problems. Free, Pro, Max, Team, and Enterprise tiers, plus API pricing for developers.
claude.com