|
5 | 5 | # Runs the keep-llm-gateway in front of the Anthropic API and demonstrates |
6 | 6 | # policy enforcement: streaming, redaction, and audit logging. |
7 | 7 | # |
8 | | -# Prerequisites: |
9 | | -# - ANTHROPIC_API_KEY set in your environment |
| 8 | +# Auth is handled by `claude -p`, which picks up credentials from wherever |
| 9 | +# you have them configured (OAuth, API key, etc.). |
10 | 10 | # |
11 | 11 | # Usage: |
12 | 12 | # ./examples/llm-gateway-demo/demo.sh |
@@ -24,11 +24,6 @@ cleanup() { |
24 | 24 | } |
25 | 25 | trap cleanup EXIT |
26 | 26 |
|
27 | | -if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
28 | | - echo "ANTHROPIC_API_KEY is not set. Export it and re-run." |
29 | | - exit 1 |
30 | | -fi |
31 | | - |
32 | 27 | echo "=== Keep LLM Gateway Demo ===" |
33 | 28 | echo "" |
34 | 29 |
|
@@ -51,91 +46,72 @@ sleep 1 |
51 | 46 | echo " Gateway running (PID $GW_PID)" |
52 | 47 | echo "" |
53 | 48 |
|
54 | | -GW_URL="http://localhost:${GW_PORT}/v1/messages" |
55 | | -GW_HEADERS=(-H "Content-Type: application/json" -H "anthropic-version: 2023-06-01" -H "x-api-key: $ANTHROPIC_API_KEY") |
| 49 | +export ANTHROPIC_BASE_URL="http://localhost:${GW_PORT}" |
56 | 50 |
|
57 | | -# ── Test 1: Streaming request ───────────────────────────────────── |
| 51 | +# ── Test 1: Streaming request via claude -p ─────────────────────── |
58 | 52 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
59 | 53 | echo "Test 1: Streaming request through the gateway" |
60 | 54 | echo "" |
61 | | - |
62 | | -RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
63 | | - -d '{ |
64 | | - "model": "claude-haiku-4-5-20251001", |
65 | | - "max_tokens": 30, |
66 | | - "stream": true, |
67 | | - "messages": [{"role": "user", "content": "What is 2+2? Answer in one word."}] |
68 | | - }' 2>&1) |
69 | | - |
70 | | -HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
71 | | -BODY=$(echo "$RESPONSE" | sed '$d') |
72 | | - |
73 | | -if [ "$HTTP_CODE" = "200" ]; then |
74 | | - EVENT_COUNT=$(echo "$BODY" | grep -c "^event:" || true) |
75 | | - echo " ✓ HTTP 200 — Received $EVENT_COUNT SSE events (buffered, policy-evaluated, replayed)" |
76 | | -else |
77 | | - echo " ✗ HTTP $HTTP_CODE — $BODY" |
78 | | - echo "" |
79 | | - echo " Check that ANTHROPIC_API_KEY is valid." |
80 | | - exit 1 |
81 | | -fi |
| 55 | +echo " \$ ANTHROPIC_BASE_URL=$ANTHROPIC_BASE_URL claude -p 'What is 2+2?'" |
82 | 56 | echo "" |
83 | 57 |
|
84 | | -# ── Test 2: Non-streaming request ───────────────────────────────── |
85 | | -echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
86 | | -echo "Test 2: Non-streaming request through the gateway" |
87 | | -echo "" |
88 | | - |
89 | | -RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
90 | | - -d '{ |
91 | | - "model": "claude-haiku-4-5-20251001", |
92 | | - "max_tokens": 30, |
93 | | - "messages": [{"role": "user", "content": "What is 2+2? Answer in one word."}] |
94 | | - }' 2>&1) |
95 | | - |
96 | | -HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
97 | | -BODY=$(echo "$RESPONSE" | sed '$d') |
98 | | - |
99 | | -if [ "$HTTP_CODE" = "200" ]; then |
100 | | - ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'])" 2>/dev/null || echo "$BODY") |
101 | | - echo " ✓ HTTP 200 — Model said: $ANSWER" |
| 58 | +if timeout 30 claude -p "What is 2+2? Answer in exactly one word, no punctuation." \ |
| 59 | + --model claude-haiku-4-5-20251001 \ |
| 60 | + --max-turns 1 2>&1; then |
| 61 | + echo "" |
| 62 | + echo " ✓ Streaming request succeeded" |
102 | 63 | else |
103 | | - echo " ✗ HTTP $HTTP_CODE — $BODY" |
| 64 | + EXIT_CODE=$? |
| 65 | + echo "" |
| 66 | + if [ "$EXIT_CODE" -eq 124 ]; then |
| 67 | + echo " ✗ Timed out after 30s" |
| 68 | + else |
| 69 | + echo " ✗ claude exited with code $EXIT_CODE" |
| 70 | + fi |
104 | 71 | fi |
105 | 72 | echo "" |
106 | 73 |
|
107 | | -# ── Test 3: Secret redaction ────────────────────────────────────── |
| 74 | +# ── Test 2: Secret redaction ────────────────────────────────────── |
108 | 75 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
109 | | -echo "Test 3: Secret redaction in tool results" |
| 76 | +echo "Test 2: Secret redaction in tool results" |
110 | 77 | echo "" |
111 | 78 | echo " Sending a tool_result containing an AWS key and password." |
112 | 79 | echo " The gateway redacts secrets before forwarding to the model." |
113 | 80 | echo "" |
114 | 81 |
|
115 | | -RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
116 | | - -d '{ |
117 | | - "model": "claude-haiku-4-5-20251001", |
118 | | - "max_tokens": 100, |
119 | | - "messages": [ |
120 | | - {"role": "user", "content": "Read the .env file"}, |
121 | | - {"role": "assistant", "content": [ |
122 | | - {"type": "tool_use", "id": "tool_1", "name": "bash", "input": {"command": "cat .env"}} |
123 | | - ]}, |
124 | | - {"role": "user", "content": [ |
125 | | - {"type": "tool_result", "tool_use_id": "tool_1", "content": "DB_HOST=localhost\nAWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\npassword = hunter2\nAPP_NAME=myapp"} |
126 | | - ]} |
127 | | - ] |
128 | | - }' 2>&1) |
129 | | - |
130 | | -HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
131 | | -BODY=$(echo "$RESPONSE" | sed '$d') |
132 | | - |
133 | | -if [ "$HTTP_CODE" = "200" ]; then |
134 | | - echo " ✓ HTTP 200 — Model responded (secrets were redacted before reaching it)" |
135 | | - ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'][:200])" 2>/dev/null || echo "(parse error)") |
136 | | - echo " Model said: $ANSWER" |
| 82 | +if [ -n "${ANTHROPIC_API_KEY:-}" ]; then |
| 83 | + RESPONSE=$(curl -s -w "\n%{http_code}" \ |
| 84 | + "$ANTHROPIC_BASE_URL/v1/messages" \ |
| 85 | + -H "Content-Type: application/json" \ |
| 86 | + -H "anthropic-version: 2023-06-01" \ |
| 87 | + -H "x-api-key: $ANTHROPIC_API_KEY" \ |
| 88 | + -d '{ |
| 89 | + "model": "claude-haiku-4-5-20251001", |
| 90 | + "max_tokens": 100, |
| 91 | + "messages": [ |
| 92 | + {"role": "user", "content": "Read the .env file"}, |
| 93 | + {"role": "assistant", "content": [ |
| 94 | + {"type": "tool_use", "id": "tool_1", "name": "bash", "input": {"command": "cat .env"}} |
| 95 | + ]}, |
| 96 | + {"role": "user", "content": [ |
| 97 | + {"type": "tool_result", "tool_use_id": "tool_1", "content": "DB_HOST=localhost\nAWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\npassword = hunter2\nAPP_NAME=myapp"} |
| 98 | + ]} |
| 99 | + ] |
| 100 | + }' 2>&1) |
| 101 | + |
| 102 | + HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
| 103 | + BODY=$(echo "$RESPONSE" | sed '$d') |
| 104 | + |
| 105 | + if [ "$HTTP_CODE" = "200" ]; then |
| 106 | + echo " ✓ HTTP 200 — Model responded (secrets were redacted before reaching it)" |
| 107 | + ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'][:200])" 2>/dev/null || echo "(parse error)") |
| 108 | + echo " Model said: $ANSWER" |
| 109 | + else |
| 110 | + echo " ✗ HTTP $HTTP_CODE — $BODY" |
| 111 | + fi |
137 | 112 | else |
138 | | - echo " ✗ HTTP $HTTP_CODE — $BODY" |
| 113 | + echo " ~ Skipped: ANTHROPIC_API_KEY not set (needed for curl-based redaction test)" |
| 114 | + echo " Redaction is verified by unit tests: make test-unit ARGS='-run TestProxy_RedactRequest'" |
139 | 115 | fi |
140 | 116 | echo "" |
141 | 117 |
|
|
169 | 145 | echo "" |
170 | 146 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
171 | 147 | echo "Demo complete." |
172 | | -echo "" |
173 | | -echo "You can also point claude at the gateway:" |
174 | | -echo " ANTHROPIC_BASE_URL=http://localhost:${GW_PORT} claude" |
175 | | -echo "" |
176 | | -echo "Response deny (blocking destructive commands) is covered by unit tests:" |
177 | | -echo " make test-unit ARGS='-run TestProxy_DenyResponse ./internal/gateway'" |