Skip to content

Commit 0153363

Browse files
Add agent_id field to analytics events and integrate usage across modules
- Introduced an optional `agent_id` field in `kb.query_executed` and `kb.content_indexed` analytics events to support per-agent KB usage metrics. - Updated event payload generation in `tilellm/analytics/events.py` to handle `agent_id`. - Integrated `agent_id` field propagation in `tilellm/models/llm.py` and `tilellm/__main__.py`.
1 parent b5772cd commit 0153363

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

tilellm/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ async def reader(channel: Redis):
277277
chunks_indexed=getattr(pc_result, "chunks", 0) or 0,
278278
error_message=_idx_error,
279279
request_id=item_single.request_id,
280+
agent_id=item_single.agent_id,
280281
)
281282
analytics.publish_nowait(_et, item_single.id_project, _pl)
282283

@@ -655,6 +656,7 @@ async def create_scrape_item_single(
655656
else 0,
656657
error_message=_idx_error,
657658
request_id=item.request_id,
659+
agent_id=item.agent_id,
658660
)
659661
analytics.publish_nowait(_et, item.id_project, _pl)
660662

@@ -772,6 +774,7 @@ async def create_scrape_item_hybrid(
772774
else 0,
773775
error_message=_idx_error,
774776
request_id=item.request_id,
777+
agent_id=item.agent_id,
775778
)
776779
analytics.publish_nowait(_et, item.id_project, _pl)
777780

@@ -838,6 +841,7 @@ async def post_ask_with_memory_main(question_answer: QuestionAnswer):
838841
reranker_model=_reranker_model,
839842
latency_ms=_chunks_latency_ms,
840843
request_id=question_answer.request_id,
844+
agent_id=question_answer.agent_id,
841845
)
842846
analytics.publish_nowait(_et, question_answer.id_project, _pl)
843847
return _chunks_result
@@ -890,6 +894,7 @@ async def post_ask_with_memory_main(question_answer: QuestionAnswer):
890894
latency_ms=0,
891895
request_id=question_answer.request_id,
892896
success=result.success,
897+
agent_id=question_answer.agent_id,
893898
)
894899
analytics.publish_nowait(_et, question_answer.id_project, _pl)
895900
return result
@@ -965,6 +970,7 @@ async def post_ask_with_memory_main(question_answer: QuestionAnswer):
965970
latency_ms=_qa_latency_ms,
966971
request_id=question_answer.request_id,
967972
success=result_success,
973+
agent_id=question_answer.agent_id,
968974
)
969975
analytics.publish_nowait(_et, question_answer.id_project, _pl)
970976
return result

tilellm/analytics/events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def kb_query(
104104
reranker_latency_ms: Optional[int] = None,
105105
request_id: Optional[str] = None,
106106
success: Optional[bool] = None,
107+
agent_id: Optional[str] = None,
107108
) -> Tuple[str, dict]:
108109
"""
109110
Build a kb.query_executed event payload.
@@ -120,6 +121,7 @@ def kb_query(
120121
request_id: Tiledesk conversation/request ID.
121122
success: Whether the LLM produced an answer from retrieved chunks.
122123
Use None when no LLM answer exists (e.g. chunks_only).
124+
agent_id: Tiledesk agent/bot ID. Used for per-agent KB usage metrics.
123125
"""
124126
payload: dict = {
125127
"kb_id": kb_id,
@@ -130,6 +132,7 @@ def kb_query(
130132
"reranker_model": reranker_model,
131133
"latency_ms": latency_ms,
132134
"request_id": request_id,
135+
"agent_id": agent_id,
133136
}
134137
if reranker_latency_ms is not None:
135138
payload["reranker_latency_ms"] = reranker_latency_ms
@@ -150,6 +153,7 @@ def content_indexed(
150153
chunks_indexed: int = 0,
151154
error_message: Optional[str] = None,
152155
request_id: Optional[str] = None,
156+
agent_id: Optional[str] = None,
153157
) -> Tuple[str, dict]:
154158
"""
155159
Build a kb.content_indexed event payload.
@@ -166,6 +170,7 @@ def content_indexed(
166170
chunks_indexed: Number of chunks successfully indexed.
167171
error_message: str(e) on exception, else null.
168172
request_id: Tiledesk conversation/request ID.
173+
agent_id: Tiledesk agent/bot ID. Used for per-agent KB usage metrics.
169174
"""
170175
return "kb.content_indexed", {
171176
"kb_id": kb_id,
@@ -179,6 +184,7 @@ def content_indexed(
179184
"success": success,
180185
"error_message": error_message,
181186
"request_id": request_id,
187+
"agent_id": agent_id,
182188
}
183189

184190

tilellm/models/llm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ class ItemSingle(BaseModel):
158158
description="Tiledesk conversation/request ID. Links indexing operations to a "
159159
"specific customer conversation in the analytics platform.",
160160
)
161+
agent_id: Optional[str] = Field(
162+
default=None,
163+
description="Tiledesk agent/bot ID for analytics. Enables per-agent KB usage metrics.",
164+
)
161165
situated_context: Optional[SituatedContextConfig] = Field(
162166
default=None,
163167
description="Dedicated LLM configuration for Contextual Retrieval (situated context). Allows using a different/cheaper LLM for generating contextual sentences.",

0 commit comments

Comments
 (0)