Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(copilot): enable OpenRouter broadcast for SDK /messages endpoint (#12277)

Zamil Majdy160d6eddfb5d
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index f367ac8c81..6891482098 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -297,13 +297,21 @@ def _resolve_sdk_model() -> str | None:
return model
-def _build_sdk_env() -> dict[str, str]:
+def _build_sdk_env(
+ session_id: str | None = None,
+ user_id: str | None = None,
+) -> dict[str, str]:
"""Build env vars for the SDK CLI process.
Routes API calls through OpenRouter (or a custom base_url) using
the same ``config.api_key`` / ``config.base_url`` as the non-SDK path.
This gives per-call token and cost tracking on the OpenRouter dashboard.
+ When *session_id* is provided, an ``x-session-id`` custom header is
+ injected via ``ANTHROPIC_CUSTOM_HEADERS`` so that OpenRouter Broadcast
+ forwards traces (including cost/usage) to Langfuse for the
+ ``/api/v1/messages`` endpoint.
+
Only overrides ``ANTHROPIC_API_KEY`` when a valid proxy URL and auth
token are both present — otherwise returns an empty dict so the SDK
falls back to its default credentials.
@@ -321,6 +329,22 @@ def _build_sdk_env() -> dict[str, str]:
env["ANTHROPIC_AUTH_TOKEN"] = config.api_key
# Must be explicitly empty so the CLI uses AUTH_TOKEN instead
env["ANTHROPIC_API_KEY"] = ""
+
+ # Inject broadcast headers so OpenRouter forwards traces to Langfuse.
+ # The ``x-session-id`` header is *required* for the Anthropic-native
+ # ``/messages`` endpoint — without it broadcast silently drops the
+ # trace even when org-level Langfuse integration is configured.
+ def _safe(value: str) -> str:
+ """Strip CR/LF to prevent header injection, then truncate."""
+ return value.replace("\r", "").replace("\n", "").strip()[:128]
+
+ headers: list[str] = []
+ if session_id:
+ headers.append(f"x-session-id: {_safe(session_id)}")
+ if user_id:
+ headers.append(f"x-user-id: {_safe(user_id)}")
+ if headers:
+ env["ANTHROPIC_CUSTOM_HEADERS"] = "\n".join(headers)
return env
@@ -706,7 +730,7 @@ async def stream_chat_completion_sdk(
set_execution_context(user_id, session, sandbox=e2b_sandbox, sdk_cwd=sdk_cwd)
try:
# Fail fast when no API credentials are available at all
- sdk_env = _build_sdk_env()
+ sdk_env = _build_sdk_env(session_id=session_id, user_id=user_id)
if not sdk_env and not os.environ.get("ANTHROPIC_API_KEY"):
raise RuntimeError(
"No API key configured. Set OPEN_ROUTER_API_KEY "
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.py
index a5dfe612de..d2ccce1a6a 100644
--- a/autogpt_platform/backend/backend/copilot/service.py
+++ b/autogpt_platform/backend/backend/copilot/service.py
@@ -1061,6 +1061,13 @@ async def _stream_chat_chunks(
:128
] # OpenRouter limit
+ # Broadcast trace metadata — forwarded to Langfuse via
+ # OpenRouter's org-level Broadcast integration.
+ extra_body["trace"] = {
+ "trace_name": "copilot-chat",
+ "environment": settings.config.app_env.value,
+ }
+
# Enable adaptive thinking for Anthropic models via OpenRouter
if config.thinking_enabled and "anthropic" in model.lower():
extra_body["reasoning"] = {"enabled": True}