You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added:
- Latency Tracking: Track execution duration for each tool call
- `@stat.track` decorator for automatic latency tracking (recommended)
- `async with stat.tracking(name, type)` context manager alternative
- `duration_ms` parameter in `record()` for manual timing
- New latency columns: `total_duration_ms`, `min_duration_ms`, `max_duration_ms`
- `latency_summary` in `get_stats()` response with total duration
- Per-tool `avg_latency_ms`, `min_duration_ms`, `max_duration_ms` metrics
Changed:
- Database schema bumped to v3 with latency tracking columns
- `get_stats()` response now includes `latency_summary` object
- Each stat item now includes latency fields
- API Improvement: `@stat.track` decorator is now the recommended way to track calls
- Eliminates the "first line" requirement
- Automatic latency measurement
- Never fails user code
Migration:
- Automatic database migration from v2 to v3
- Preserves all existing data
- New latency columns default to 0/NULL for existing records
For cases where you need more control than a decorator:
72
+
73
+
```python
74
+
asyncdefhandle_tool(name: str, arguments: dict):
75
+
asyncwith stat.tracking(name, "tool"):
76
+
result =await my_logic(arguments)
77
+
return result
78
+
```
79
+
80
+
---
81
+
37
82
### record()
38
83
39
-
Record a tool, prompt, or resource invocation.
84
+
Low-level method for manual recording. Use `@stat.track` instead when possible.
40
85
41
86
```python
42
87
await stat.record(
@@ -48,6 +93,7 @@ await stat.record(
48
93
response_chars: int|None=None,
49
94
input_tokens: int|None=None,
50
95
output_tokens: int|None=None,
96
+
duration_ms: int|None=None,
51
97
)
52
98
```
53
99
@@ -60,18 +106,10 @@ await stat.record(
60
106
|`response_chars`|`int`| Response size for token estimation |
61
107
|`input_tokens`|`int`| Actual input token count |
62
108
|`output_tokens`|`int`| Actual output token count |
109
+
|`duration_ms`|`int`| Execution duration in milliseconds |
63
110
64
-
> **Critical**: Always call `record()` as the **FIRST line** in your handlers to guarantee 100% tracking coverage.
65
-
66
-
**Example:**
67
-
68
-
```python
69
-
@app.call_tool()
70
-
asyncdefhandle_tool(name: str, arguments: dict):
71
-
await stat.record(name, "tool") # FIRST LINE
72
-
result =await my_logic(arguments)
73
-
return result
74
-
```
111
+
!!! note "When to use record()"
112
+
Use `record()` directly only when you need to pass additional data like `response_chars` or `input_tokens`. For basic tracking with automatic latency, use `@stat.track` (decorator) or `stat.tracking()` (context manager) instead.
0 commit comments