devika icon indicating copy to clipboard operation
devika copied to clipboard

Token Usage should be inferred from `AgentState` instead of Lazy initialized global value (`TOKEN_USAGE`)

Open mufeedvh opened this issue 11 months ago • 2 comments

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

mufeedvh avatar Mar 21 '24 15:03 mufeedvh

Hello @mufeedvh , Resolved this raising PR.

ketangangal avatar Mar 24 '24 11:03 ketangangal

Here: https://github.com/stitionai/devika/pull/112

ketangangal avatar Mar 24 '24 12:03 ketangangal