elm.ords.services.usage.UsageTracker

class UsageTracker(label, response_parser)[source]

Bases: UserDict

Rate or AIP usage tracker.

Parameters:
  • label (str) – Top-level label to use when adding this usage information to another dictionary.

  • response_parser (callable) – A callable that takes the current usage info (in dictionary format) and an LLm response as inputs, updates the usage dictionary with usage info based on the response, and returns the updated dictionary. See, for example, elm.ords.services.openai.usage_from_response().

Methods

add_to(other)

Add the contents of this usage information to another dict.

clear()

copy()

fromkeys(iterable[, value])

get(k[,d])

items()

keys()

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem()

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[,d])

update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

update_from_model([response, sub_label])

Update usage from a model response.

values()

Attributes

totals

Compute total usage across all sub-labels.

add_to(other)[source]

Add the contents of this usage information to another dict.

The contents of this dictionary are stored under the label key that this object was initialized with.

Parameters:

other (dict) – A dictionary to add the contents of this one to.

property totals

Compute total usage across all sub-labels.

Returns:

dict – Dictionary containing usage information totaled across all sub-labels.

update_from_model(response=None, sub_label='default')[source]

Update usage from a model response.

Parameters:
  • response (object, optional) – Model call response, which either contains usage information or can be used to infer/compute usage. If None, no update is made.

  • sub_label (str, optional) – Optional label to categorize usage under. This can be used to track usage related to certain categories. By default, "default".

clear() None.  Remove all items from D.
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values