Token usage from a flow execution?

Hello,

Can anyone explain how to get the final total token usage by the various crews at the end of a flow execution?

I have read this doc, but can’t figure out how I would extract this token usage information with a flow, like the write_a_book_with_flows example.

Thanks,
Jeff

1 Like

@Jeff_Chen I suggest you use AgentOps, which is a Python SDK for AI agents monitoring, including information about token usage and cost. It takes just 2 lines of code!

1 Like

Thank you for our suggestion, @rokbenko.

I also realized that I could save token_usage from the crew output into my state variable then summarize the token usage at the end of the flow.

Hi @Jeff_Chen,

Can you please help me with save token_usage from the crew output into state. If you have any documentation for that or you could simply write dummy code then it would be very beneficial. Waiting for your reply,

Thanks

class MainFlowState(BaseModel):
    user_msg: str = ""
    total_token_usage: int = 0 


class MainFlow(Flow[MainFlowState]):
    
    temp = AICrew(available_data=other_knowledge_base).crew(),
      

    @start()
    def start_manager(self):
        print("Started Flow")
        
        result = (
            self.temp
            .kickoff(inputs={
            "user_msg": self.state.user_msg,
            ),
            }))

        self.state.total_token_usage += result.token_usage.total_tokens

Something like this @Akshit_Trivedi

Remember to set it to 0 at the end if you are using the same flow state again.