Needed: Cost and token tracking in agent sdk
# proj-sdk
s
Needed: Cost and token tracking in agent sdk
c
Would
event.metrics
help here?
ah that reminds me we need title generation mechanism too
s
Thanks -
event.metrics
seems to be exactly what I need
But yes - I'll need a title too
(I'm not sure if title is something that should be done for all conversations (does the CLI even use it?) - Maybe some optional call? After a preset number of events or time maybe I can call something like:
title = conversation.generate_title()
nod 1
c
BTW i wonder if it make sense for us to have one single source for truth for server, can you also check if there's any other endpoint you want here? https://github.com/All-Hands-AI/agent-sdk/blob/feature/agent-sdk-server-api/openhands/server/openapi.json
(i guess at least we can have the agent-sdk routes to be a subset of our overall server routes?
if that make sense to do 🙂
s
I agree there should be one source for truth - I'm not sure if the server code belongs in agent sdk though - I had created an OpenHands-Server project (Based on the OpenHands-CLI one Rohit created). But I'm not sure this is the final location for the code - and everything will be up in the air. So far I have been going with: • I do not expose the term "Runtime" anywhere - I use "Sandbox" exclusively. Sandboxed Server Mimics our current imp which does not have a concept of users. All access control is via a session API key. • Local Events - clients can read these via REST, post events, and subscribe to streams via websocket • Local Conversations - Clients can start / pause / resume conversations. Has additional concept of webhooks (for passing data back to the managing server / an event bridge if we use one) Managing Server This mimics our current SAAS server - though I now insist on a user id which will be a default in OSS • Sandboxes : Clients can get / search /start / pause / delete these. • Sandbox Specs Clients can get / search these. (The eventual idea is to allow building / uploading sandbox specs, and managing warm runtimes. • Sandboxed Conversations - Similar to local conversations but read only and with a Sandbox ID - Go to the sandbox itself to start / pause / resume • Sandboxed Events - Similar to local events but read only and no websocket subscriptions - Go to the sandbox itself for that.
👍 1
The general idea here is we start with our existing pattern - one conversation per sandbox. But the API allows us to do more efficient things as we figure out a good UI for them
c
I think it makes sense for MOST of the server to be in the application layer, the only thing I'm worried about is related to conversation logic / input/output, I guess we can collaborate on https://github.com/All-Hands-AI/OpenHands-Server/tree/main/openhands_server/local_conversation -- and maybe port in the current implementation I had for agent-SDK. Conversation, especially conversation start will probably be the main interface between agent team and app team, and i'd hope we can figure out an interface we are both happy with, i.e., support product w/o making it painfully hard for agent team to iterate
I kinda like the sandbox pattern
The main reason I'm writing a server in SDK is that we have a pressing need for a server to run eval on 😅 . Basically we really want the "Managing Server" part to be exposed as an API we can use to launch eval
but if we are getting close in "OpenHands-Server", i'd be happy to dump the work there and merge the current stuff we have into OpenHands-Server if that makes more sense!
s
Let's see where we are with it tomorrow morning!
👍 1
One thing I need to port over is passing secrets into the sandbox - that is one reason I figured that this belonged in server rather than agent sdk
And git details too
c
for secrets we might need a new routes, rohit got the secret manager PR merged into sdk so it shouldn't be too hard to support that as well!
w
I think @quick-window-91705 had a solution here
nod 1
IIRC that was why we put ConversationMetrics and LLMRegistry in the SDK
c
anyway! let's discuss this more tmr, and we can figure it out! I think the main pain point for the agent team is that we lack a "server" that we can easily run via docker/remote runtime to perform eval, we have basically two choice: 1. develop our own mini server for gaent-sdk and use it for eval; but we also need to implement the SAME sandbox spec/build/launch which seems to be excessive 2. OR wait until Openhands-Server is complete and the agent team can just re-use it to launch eval
w
Yeah I keep going back and forth on the mini-server
c
I got the mini server working locally in my branch, and the moment i try to get docker build to work, i realize it probably belongs to the bigger server 😅
w
nod but it's a good POC
and I could see a good argument for a mini-server that has no data persistence
s
It would need extra dependencies which I feel violates the principals of agent sdk - fastapi, socketio to name a few
w
agree--it shouldn't go in the core SDK
c
right now, the mini server is its own python package outside the sdk though
👍 2
but i think overall having two server is definitely confusing..
nod 2
c
A few thoughts • I think CLI will need auto-titles, at least for telling the user what conversation they auto-resumed, or showing the recent conversations and picking one to resume • this doesn’t sound entirely right “a user id which will be a default in OSS” •
event.metrics
is a bit strange, I guess we imported it from V0, but maybe it’s worth thinking to move it per conversation, rather than per event? Reason is I think of tokens more as dynamic data, data that can change at next conversation restore, for various reasons (llm switch, but also regular condensation) and then the sum per conversation may not match whatever we saved per event. Unless those per event metrics are dynamic too?
c
event.metrics
is a bit strange, I guess we imported it from V0, but maybe it’s worth thinking to move it per conversation, rather than per event?
this is basically a
snapshot
that only includes the cumulative tokens consuption up until this point - it is kinda handy for CLI/GUI to visualize real-time cost tho
c
My thinking here is that it will be false after a condensation, right? So we shouldn’t maybe save it / serialize it, then?
A snapshot for visualization is cool
s
Copy code
this doesn't sound entirely right "a user id which will be a default in OSS"
Explanation At the moment we have a lot of code to accommodate that the user_id may be
None
in OSS. Instead we will have a constant like:
Copy code
OSS_USER_ID = UUID('00000000-0000-0000-0000-000000000001')
Internally we will treat all installations as a multi user system (if we add a permissions model with superadmin, the OSS user becomes a superadmin). When the initial install / startup is done in a single user mode, we set up this user in the persistence layer. This means we don't have special cases in our authorization code - we effectively have one path rather than two
I think It will make it easier moving forward to handle situations like allowing multiple users to participate in the same conversation or allowing a conversation to be shared in read only mode.
(of course, we may not even need the constant)
c
What I propose here is that we don’t use hardcoded primitives like
user_id
to write most code against: instead decide on a abstraction that the various clients (local, remote) implement for the user context, and use that, e.g.
UserContext
or
RequestContext
. That’s particularly interesting when what this per-user does is storage isolation, because the weird thing today is that the V0 code is full of
if user_id
which serves only that purpose. But we could avoid to write code against a primitive string, because then we need to litter it everywhere there is storage involved. https://github.com/All-Hands-AI/OpenHands/issues/10850
👀 1
s
I like this proposal
🙏 1
c
(Sorry that topic contains actually two different things, a SU mode, and the design of APIs; we can ignore completely the SU mode, the API design proposal stands on its own, doesn’t depend on the first)
I think It will make it easier moving forward to handle situations like allowing multiple users to participate in the same conversation or allowing a conversation to be shared in read only mode.
That’s just the point: if code depends on an auth primitive (
user_id
) and we want to add an
org_id
, I’m not sure how we’d do that, without it creeping in multiple places, in actual business logic. Idk, we don’t really want to write code like
if user_id and org_id
, do we? 😅 Instead I simply suggest that routes depend on a few abstractions, which both OH and the Saas can implement, and which encapsulate the auth-y thing like user_id, e.g. “context object from which we can retrieve locations you can access”
nod 1
c
@straight-sundown-84319, maybe @wooden-umbrella-84098, do y'all have time this morning for a quick sync on potentially merging the Agent-SDK server with/ OpenHands Server? If not, maybe we can sync on office hour
w
Yeah I’m around
Tim may not be in for a bit, maybe after exec call
c
👍 I'm also ok between 10am-11am