Skip to content

Commit

Permalink
feat: Add token usage counter
Browse files Browse the repository at this point in the history
  • Loading branch information
whiterabbit1983 committed Oct 14, 2024
1 parent ccac99c commit 3c77808
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion agents-api/agents_api/metrics/counters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def decor(func: Callable[P, T] | None = None):
)

@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs):
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
metric.labels(kwargs.get(id_field_name, "not_set")).inc()
return func(*args, **kwargs)

Expand Down
5 changes: 5 additions & 0 deletions agents-api/agents_api/routers/sessions/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ...models.chat.gather_messages import gather_messages
from ...models.chat.prepare_chat_context import prepare_chat_context
from ...models.entry.create_entries import create_entries
from .metrics import total_tokens_per_user
from .router import router


Expand Down Expand Up @@ -166,4 +167,8 @@ async def chat(
choices=[choice.model_dump() for choice in model_response.choices],
)

total_tokens_per_user.labels(str(developer.id)).inc(
amount=chat_response.usage.total_tokens or 0
)

return chat_response
7 changes: 7 additions & 0 deletions agents-api/agents_api/routers/sessions/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from prometheus_client import Counter

total_tokens_per_user = Counter(
"total_tokens_per_user",
"Total token count per user",
labels=("developer_id",),
)

0 comments on commit 3c77808

Please sign in to comment.