Skip to content

Commit 015fe51

Browse files
committed
feat: Enhance gateway security features and update documentation
- Added input firewall and output moderation options in README.md. - Introduced `get_gateway_security_summary()` method in CostKatanaClient for fetching aggregated security stats. - Updated __init__.py to include new gateway-related exports.
1 parent 61ef6dd commit 015fe51

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ The Python package’s high-level **`ck.ai()`** / **`ck.chat()`** APIs talk to C
5555
| `Content-Type` | `application/json` |
5656
| `x-project-id` | Optional — same as `PROJECT_ID` for dashboard scoping |
5757

58+
The hosted gateway enables **input firewall** (LLM security) and **output moderation** by default. To opt out for a request, add `CostKatana-LLM-Security-Enabled: false` and/or `CostKatana-Output-Moderation-Enabled: false`. In Python you can merge `cost_katana.gateway_request_headers(llm_security_enabled=False)` into your headers.
59+
60+
To fetch dashboard aggregates (blocked prompts, moderation counts), use `CostKatanaClient.get_gateway_security_summary()` (`GET /api/gateway/security/summary`).
61+
5862
**OpenAI-compatible**`POST {GATEWAY}/v1/chat/completions`
5963

6064
```python

cost_katana/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Optional, List, Dict, Any
2323

2424
from .client import CostKatanaClient, get_global_client, configure, auto_configure, from_env
25+
from .gateway import gateway_request_headers, GATEWAY_API_PREFIX
2526
from .models import ChatSession
2627
from .exceptions import (
2728
CostKatanaError,
@@ -439,4 +440,7 @@ def _infer_provider(model: str) -> str:
439440
"CostLimitExceededError",
440441
# Config
441442
"Config",
443+
# Gateway (direct HTTP to /api/gateway)
444+
"gateway_request_headers",
445+
"GATEWAY_API_PREFIX",
442446
]

cost_katana/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .logging import AILogger
1919
from .logging.logger import logger
2020
from .templates import TemplateManager
21+
from .gateway import GATEWAY_API_PREFIX
2122

2223
# Global client instance for the configure function
2324
_global_client = None
@@ -354,3 +355,18 @@ def delete_conversation(self, conversation_id: str) -> Dict[str, Any]:
354355
if isinstance(e, CostKatanaError):
355356
raise
356357
raise CostKatanaError(f"Failed to delete conversation: {str(e)}")
358+
359+
def get_gateway_security_summary(self) -> Dict[str, Any]:
360+
"""
361+
Fetch aggregated gateway security stats (input firewall ThreatLog + output moderation usage).
362+
363+
Calls ``GET /api/gateway/security/summary`` with the same auth as other dashboard APIs.
364+
"""
365+
try:
366+
response = self.client.get(f"{GATEWAY_API_PREFIX}/security/summary")
367+
data = self._handle_response(response)
368+
return data.get("data", data)
369+
except Exception as e:
370+
if isinstance(e, CostKatanaError):
371+
raise
372+
raise CostKatanaError(f"Failed to get gateway security summary: {str(e)}")

cost_katana/gateway.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
AI Gateway helpers — HTTP headers and dashboard APIs aligned with Cost Katana gateway defaults.
3+
4+
The hosted gateway enables input firewall (LLM security) and output moderation by default.
5+
Send explicit ``false`` headers only to opt out.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from typing import Any, Dict, Optional
11+
12+
# Relative to API origin (e.g. https://api.costkatana.com)
13+
GATEWAY_API_PREFIX = "/api/gateway"
14+
15+
16+
def gateway_request_headers(
17+
*,
18+
llm_security_enabled: Optional[bool] = None,
19+
output_moderation_enabled: Optional[bool] = None,
20+
) -> Dict[str, str]:
21+
"""
22+
Extra HTTP headers for direct ``POST /api/gateway/v1/...`` calls (e.g. with httpx).
23+
24+
Server defaults: both protections ON. Pass ``False`` to opt out.
25+
26+
Args:
27+
llm_security_enabled: If False, sets ``CostKatana-LLM-Security-Enabled: false``.
28+
output_moderation_enabled: If False, sets ``CostKatana-Output-Moderation-Enabled: false``.
29+
"""
30+
h: Dict[str, str] = {}
31+
if llm_security_enabled is False:
32+
h["CostKatana-LLM-Security-Enabled"] = "false"
33+
if output_moderation_enabled is False:
34+
h["CostKatana-Output-Moderation-Enabled"] = "false"
35+
return h

0 commit comments

Comments
 (0)