Base
base
Base classes used across the different LLM endpoint types offered by LLMeter
You can also use these classes to implement your own custom Endpoint integrations.
Endpoint
Endpoint(endpoint_name, model_id, provider)
Bases: ABC, Generic[TRawResponse]
An abstract base class for endpoint implementations.
We strongly recommend using the
llmeter_invoke decorator to implement
custom endpoints as shown below - which wraps payload pre-processing, response parsing, and
error handling around a core invoke function you provide.
Example
class MyCustomEndpoint(Endpoint[MyAISDKRawReturnType]):
@Endpoint.llmeter_invoke
def invoke(self, payload: dict) -> MyAISDKRawReturnType:
# Just the raw AI / SDK call goes here:
raw: MyAISDKRawReturnType = self._my_cool_api_client.call(**payload)
return raw
def process_raw_response(
self,
raw_response: MyAISDKRawReturnType,
start_t: float,
response: InvocationResponse
):
# llmeter_invoke wrapper automatically calls process_raw_response,
# in which you should parse the outputs onto `response`
response.id = raw_response["ResponseId"]
...
See llmeter_invoke and
process_raw_responsefor more info.
You can also implement:
create_payloadconvenience method to simplify building payload objects for your endpoint - for example converting a simple input prompt to a full request object with other required parameters.prepare_payloadin case you need to do any request payload pre-processing outside the timer that measures response speed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_name
|
str
|
The name of the endpoint. |
required |
model_id
|
str
|
The identifier of the model associated with this endpoint. |
required |
provider
|
str
|
The provider of the endpoint. |
required |
Source code in llmeter/endpoints/base.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
__subclasshook__
classmethod
__subclasshook__(C)
Determine if a class is considered a subclass of BaseEndpoint.
This method is used to implement a custom subclass check. A class is considered a subclass of BaseEndpoint if it has an 'invoke' method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
C
|
type
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
bool or NotImplemented: True if the class is a subclass, False if it isn't, or NotImplemented if the check is inconclusive. |
Source code in llmeter/endpoints/base.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
create_payload
staticmethod
create_payload(*args, **kwargs)
Create a payload for the endpoint invocation.
This static method should be implemented by subclasses to define
how the payload is created based on the given arguments. Ideally,
subclasses should conform to the conventions of existing endpoint types
(for example taking a user_message: str | list[ContentItem] param),
but this is not strictly enforced at the typing level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Variable length argument list. |
()
|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
NotImplemented |
Any
|
This method returns NotImplemented in the base class. |
Source code in llmeter/endpoints/base.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
invoke
abstractmethod
invoke(payload)
Call a model and return a full parsed response with error handling
Info
We strongly encourage to use the
llmeter_invoke decorator to implement
your invoke method with proper orchestration and error handling.
Endpoint.invoke should:
- Call
prepare_payloadto transform the input payload - Invoke your actual target endpoint
- Parse the results onto an
InvocationResponseobject (preferably viaprocess_raw_response) - Populate
.errorand as many other response fields as possible, in the event that an error occurs during model calling or response processing
The llmeter_invoke decorator handles this flow for you - so you'll need to re-implement
the steps if you choose not to use it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
The input payload for the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
InvocationResponse
|
The final |
Source code in llmeter/endpoints/base.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
llmeter_invoke
classmethod
llmeter_invoke(call_endpoint)
Wrap a raw model API call with pre+postprocessing and error handling
This decorator wraps around a function that only does the core model call, to add the
full range of steps that LLMeter Endpoints are expected to handle as part of invoke:
- Before starting the response timer, calls your class'
prepare_payloadmethod to transform the input payload, if required - Initialises an
InvocationResponsewith the timestamp of the request. - Calls the wrapped function to fetch the raw API response
- Calls your class'
process_raw_responsemethod to incrementally parse fields from the raw response to the targetInvocationResponse - In case of any unhandled errors during API call or response processing, logs and sets
response.error - Automatically backfills the following fields on the parsed response, if missing:
id(as a generated UUID)input_payload(the final payload sent to the API)input_prompt(via_parse_payloadmethod)time_to_last_token
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call_endpoint
|
Callable[..., TRawResponse]
|
The function to wrap. Should be a method that takes a |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., InvocationResponse]
|
A wrapped function that implements the full |
Source code in llmeter/endpoints/base.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
load
classmethod
load(endpoint_config)
Load an endpoint configuration from a dictionary.
This class method reads a dictionary containing an endpoint configuration, determines the appropriate endpoint class, and instantiates it with the loaded configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_config
|
Dict
|
A dictionary containing the endpoint configuration. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Endpoint |
Endpoint
|
An instance of the appropriate endpoint class, initialized with the configuration from the dictionary. |
Source code in llmeter/endpoints/base.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
load_from_file
classmethod
load_from_file(input_path)
Load an endpoint configuration from a JSON file.
This class method reads a JSON file containing an endpoint configuration, determines the appropriate endpoint class, and instantiates it with the loaded configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str | UPath
|
The path to the JSON configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Endpoint |
Endpoint
|
An instance of the appropriate endpoint class, initialized with the configuration from the file. |
Source code in llmeter/endpoints/base.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
prepare_payload
prepare_payload(payload)
Transform the payload before sending it to the API.
You can use it to enforce any transformations you need between the input dataset/payload
and what actually gets sent to the model, that should not be counted in the response time
measurement. For example: Setting fixed parameters required by the endpoint e.g.
streaming: False.
This method is called by the
llmeter_invoke wrapper before
starting the timer that measures response latency.
Warning
If you made a custom :meth:invoke implementation without using the
:meth:llmeter_invoke decorator - check whether your implementation actually calls
this prepare_payload method or not!
The default implementation returns payload unchanged
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
The raw input payload from the caller. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The final payload to send to the API. |
Source code in llmeter/endpoints/base.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
process_raw_response
abstractmethod
process_raw_response(raw_response, start_t, response)
Parse a raw API response onto InvocationResponse fields
Subclasses implement this to extract LLMeter data points (such as time to first and last token, output text, number of input/output tokens, etc.) from raw model responses.
Warning
If you made a custom :meth:invoke implementation without using the
:meth:llmeter_invoke decorator - check whether your implementation actually calls
this process_raw_response method or not!
This function does not return a value, but is instead expected to incrementally populate
properties on the provided draft response object.
In this way, partial data will be stored even if an error occurs later during processing. For example if a stream times out, or a guardrail intervenes - we might still be able to capture a unique ID initially pulled from the response header.
See llmeter_invoke for more details
about which fields of InvocationResponse are automatically populated for you.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_response
|
TRawResponse
|
The raw response object (returned by your |
required |
start_t
|
float
|
|
required |
response
|
InvocationResponse
|
The LLMeter response object to be populated in-place. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If something goes wrong during response streaming or parsing,
implementations can just raise an error. The :meth: |
Source code in llmeter/endpoints/base.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
save
save(output_path)
Save the endpoint configuration to a JSON file.
This method serializes the endpoint's configuration (excluding private attributes) to a JSON file at the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
str | UPath
|
The path where the configuration file will be saved. |
required |
Returns:
| Type | Description |
|---|---|
UPath
|
None |
Source code in llmeter/endpoints/base.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
to_dict
to_dict()
Convert the endpoint configuration to a dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict
|
A dictionary representation of the endpoint configuration. |
Source code in llmeter/endpoints/base.py
495 496 497 498 499 500 501 502 503 504 | |
InvocationResponse
dataclass
InvocationResponse(response_text, input_payload=None, id=None, input_prompt=None, time_to_first_token=None, time_to_last_token=None, num_tokens_input=None, num_tokens_output=None, num_tokens_input_cached=None, num_tokens_output_reasoning=None, time_per_output_token=None, error=None, retries=None, request_time=None)
A class representing a invocation result.
Attributes:
| Name | Type | Description |
|---|---|---|
response_text |
str
|
The invocation output. |
id |
str
|
A unique identifier for the invocation. |
time_to_last_token |
float
|
The time taken to generate the response in seconds. |
time_to_first_token |
float
|
The time taken to receive the first token of the response in seconds. |
num_tokens_output |
Optional[int]
|
The number of tokens in the response. |
num_tokens_input |
Optional[int]
|
The number of tokens in the invocation payload. |
num_tokens_input_cached |
int | None
|
The number of input tokens served from cache (prompt caching). |
num_tokens_output_reasoning |
int | None
|
The number of output tokens used for internal reasoning
(included in |
input_prompt |
str
|
The input prompt used in the invocation. |
time_per_output_token |
float
|
The average time taken to generate each token in the response. |
error |
str
|
Any error that occurred during invocation. |
request_time |
datetime | None
|
The wall-clock time when the request was sent. |
from_json
classmethod
from_json(json_str)
Deserialize a JSON string into an InvocationResponse.
This is the inverse of to_json. It
correctly restores types that the default JSON round-trip would leave as strings or marker
objects:
request_timeis parsed from an ISO-8601 string back to a Pythondatetimepayloads containingbytes(as__llmeter_bytes__markers) are correctly loaded back as bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
A JSON string representation of an InvocationResponse (produced by |
required |
Returns:
| Name | Type | Description |
|---|---|---|
InvocationResponse |
InvocationResponse
|
The deserialized response. |
Example
A round-trip can be run as follows:
original = InvocationResponse(response_text="hi", ...)
restored = InvocationResponse.from_json(original.to_json())
Source code in llmeter/endpoints/base.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_dict
to_dict()
Return a dictionary representation of this response.
Returns a plain dict produced by dataclasses.asdict, preserving native Python types
(e.g. datetime for request_time). This is suitable for programmatic access — for
example RunningStats consumes this output and relies on
datetime comparisons and arithmetic.
For JSON output, use to_json, (which
delegates to llmeter_default_serializer
by default, for non-JSON-serializable data types).
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary of response fields with native Python types. |
Source code in llmeter/endpoints/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
to_json
to_json(default=llmeter_default_serializer, **kwargs)
Serialize this response to a JSON string.
Uses llmeter_default_serializer by
default, which handles bytes, datetime, PathLike, and other common non-serializable
types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Fallback serializer passed to |
llmeter_default_serializer
|
|
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
JSON representation of the response. |
Source code in llmeter/endpoints/base.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |