Skip to content

Commit 588e5e4

Browse files
committed
feat: add X-Agent-Name and X-Project-Name headers to Minitap LLM calls
Enable Langfuse trace filtering by agent for cost/usage analysis. - Add _build_custom_headers() helper in services/llm.py - Add agent_name parameter to get_minitap_llm() - Pass agent name from get_llm() dispatcher to Minitap provider - Add PROJECT_NAME optional env var to Settings - Headers only sent on Minitap provider (not OpenAI/OpenRouter/Grok)
1 parent 60a8be0 commit 588e5e4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

minitap/mobile_use/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Settings(BaseSettings):
3333

3434
MOBILE_USE_TELEMETRY_ENABLED: bool | None = None
3535

36+
PROJECT_NAME: str | None = None
37+
3638
model_config = {"env_file": ".env", "extra": "ignore"}
3739

3840

minitap/mobile_use/services/llm.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ async def invoke_llm_with_timeout_message[T](
5555
return await llm_task
5656

5757

58+
def _build_custom_headers(agent_name: str | None = None) -> dict[str, str]:
59+
"""Build custom headers for LLM API calls."""
60+
headers: dict[str, str] = {}
61+
if agent_name:
62+
headers["X-Agent-Name"] = agent_name
63+
if settings.PROJECT_NAME:
64+
headers["X-Project-Name"] = settings.PROJECT_NAME
65+
return headers
66+
67+
5868
def get_minitap_llm(
5969
trace_id: str,
6070
remote_tracing: bool = False,
6171
model: str = "google/gemini-2.5-pro",
6272
temperature: float | None = None,
6373
max_retries: int | None = None,
6474
api_key: str | None = None,
75+
agent_name: str | None = None,
6576
) -> ChatOpenAI:
6677
if api_key:
6778
effective_api_key = SecretStr(api_key)
@@ -77,6 +88,9 @@ def get_minitap_llm(
7788

7889
if max_retries is None and model.startswith("google/"):
7990
max_retries = 2
91+
92+
custom_headers = _build_custom_headers(agent_name)
93+
8094
client = ChatOpenAI(
8195
model=model,
8296
temperature=temperature,
@@ -87,6 +101,7 @@ def get_minitap_llm(
87101
"sessionId": trace_id,
88102
"traceOnlyUsage": remote_tracing,
89103
},
104+
default_headers=custom_headers or None,
90105
)
91106
return client
92107

@@ -223,6 +238,7 @@ def get_llm(
223238
model=llm.model,
224239
temperature=temperature,
225240
api_key=ctx.minitap_api_key,
241+
agent_name=name,
226242
)
227243
else:
228244
raise ValueError(f"Unsupported provider: {llm.provider}")

0 commit comments

Comments
 (0)