Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Calculate token usage statistics using call stack #117 #118

Closed
wants to merge 1 commit into from

Conversation

astsiapanay
Copy link
Collaborator

No description provided.

@astsiapanay astsiapanay marked this pull request as draft January 8, 2024 10:30
@astsiapanay astsiapanay removed the request for review from artsiomkorzun January 8, 2024 10:31
@astsiapanay astsiapanay self-assigned this Jan 8, 2024
callInfo.childSpanIds.add(childSpanId);
}

public synchronized void setTokeUsage(String spanId, TokenUsage tokenUsage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct type: setTokenUsage

public void calculateTokenUsage(ProxyContext context) {
Entity entity = getEntityFromTracingContext(context);
if (entity == null) {
log.warn("Entity is not found by traceId={}", context.getTraceId());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add prefix to locate the problem more easily: "Failed to calculate token usage"

public synchronized void link(String parentSpanId, String childSpanId) {
CallInfo callInfo = spanIdToCallInfo.get(parentSpanId);
if (callInfo == null) {
log.warn("Parent span is not found by id: {}", parentSpanId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please improve logging in this class.

log.warn("Entity is not found by traceId={}", context.getTraceId());
return;
}
context.setTokenUsage(entity.calculate(context.getCurrentSpanId()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to do it only if it is missing? Some applications/models can compute it more precisely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rely on applications/assistants any longer to compute statistics

private final List<String> roles;
private final String name;
private final boolean user;
private final Map<String, CallInfo> spanIdToCallInfo = new HashMap<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the logic related to token tracking to a separate class. e.g. TokenTracker

@astsiapanay
Copy link
Collaborator Author

no longer valid since the core is moved to per request key authorization

@astsiapanay astsiapanay deleted the issue-117 branch January 15, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants