devika
devika copied to clipboard
Token Usage should be inferred from `AgentState` instead of Lazy initialized global value (`TOKEN_USAGE`)
Currently, the Token Usage is only calculated for an active session on a Lazy initialized global value.
Implement Token Usage updates into the AgentState
store.
The value is already there:
def new_state(self):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return {
"internal_monologue": None,
"browser_session": {
"url": None,
"screenshot": None
},
"terminal_session": {
"command": None,
"output": None,
"title": None
},
"step": None,
"message": None,
"completed": False,
"agent_is_active": True,
"token_usage": 0,
"timestamp": timestamp
}
Just make a helper function to update the Token Usage to the latest state and use that for the API endpoint:
@app.route("/api/token-usage", methods=["GET"])
def token_usage():
from src.llm import TOKEN_USAGE
return jsonify({"token_usage": TOKEN_USAGE})
Files:
-
LLM
: https://github.com/stitionai/devika/blob/main/src/llm/llm.py -
AgentState
: https://github.com/stitionai/devika/blob/main/src/state.py -
API
: https://github.com/stitionai/devika/blob/main/devika.py
Hello @mufeedvh , Resolved this raising PR.
Here: https://github.com/stitionai/devika/pull/112