Skip to content

Conversation

@suluyana
Copy link
Collaborator

Change Summary

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Run pre-commit install and pre-commit run --all-files before git commit, and passed lint check.
  • Documentation reflects the changes where applicable

@gemini-code-assist
Copy link

Summary of Changes

Hello @suluyana, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a suite of improvements focused on enhancing agent operational transparency and efficiency. It integrates robust token usage tracking for large language models, refines the agent's conversational memory management for more precise state handling, and optimizes resource loading for external tools. These changes collectively aim to provide deeper insights into agent performance and streamline its interaction with various components.

Highlights

  • Token Usage Tracking: Implemented global tracking for prompt and completion tokens within the LLMAgent, providing both per-step and cumulative usage logs for better monitoring of LLM interactions.
  • Enhanced Memory Management: Refined the DefaultMemory component with a new parse_messages method for role-based message filtering and improved logic for analyzing and synchronizing message blocks in memory, ensuring more accurate and efficient memory updates.
  • Asynchronous Memory Addition: Modified the LLMAgent to handle memory additions after task completion asynchronously using asyncio.run and run_in_executor, preventing potential blocking of the main event loop and improving responsiveness.
  • Optimized Tool Imports: Moved aiohttp imports from global scope to local function scope within image and video generation tools, promoting lazy loading and better dependency management.
  • OpenAI LLM Streaming Usage: Added logic to the OpenAI LLM integration to automatically enable include_usage in stream_options when streaming is active and usage tracking is desired, ensuring comprehensive token reporting for streaming responses.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@suluyana suluyana closed this Dec 26, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces token usage tracking for agents and includes several refactorings and bug fixes in the memory management component. My review focuses on improving the design of the state management for token counting, simplifying the execution of background tasks, and enhancing code readability in the memory module.

Key feedback points:

  • The use of global variables for token counting should be replaced with an encapsulated state to improve modularity and testability.
  • The method for running background async tasks is overly complex and can be greatly simplified using asyncio.create_task.
  • A method in the memory module can be refactored for better clarity.

Comment on lines +679 to +685
def _add_memory():
asyncio.run(
self.add_memory(
messages, add_type='add_after_task', **kwargs))

loop = asyncio.get_running_loop()
loop.run_in_executor(None, _add_memory)

Choose a reason for hiding this comment

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

critical

The current implementation for running add_memory in the background is overly complex and potentially problematic. Using loop.run_in_executor to call a function that itself uses asyncio.run() creates a new event loop within a worker thread. This is an unconventional pattern that can be inefficient and lead to subtle issues.

For running a "fire-and-forget" async task from within an async function, asyncio.create_task() is the standard and much simpler approach. It schedules the coroutine to run on the current event loop without blocking.

Suggested change
def _add_memory():
asyncio.run(
self.add_memory(
messages, add_type='add_after_task', **kwargs))
loop = asyncio.get_running_loop()
loop.run_in_executor(None, _add_memory)
# Schedule add_memory to run in the background without blocking.
asyncio.create_task(
self.add_memory(messages, add_type='add_after_task', **kwargs))

Comment on lines +31 to +33
TOTAL_PROMPT_TOKENS = 0
TOTAL_COMPLETION_TOKENS = 0
TOKEN_LOCK = asyncio.Lock()

Choose a reason for hiding this comment

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

high

The use of module-level global variables TOTAL_PROMPT_TOKENS and TOTAL_COMPLETION_TOKENS for tracking token usage introduces tight coupling between all agent instances within the same process. This can lead to incorrect accounting if multiple independent agents are running, and makes the code harder to test and maintain.

A better approach would be to encapsulate this state within a dedicated usage tracking class or within the LLMAgent instance itself. This would provide better isolation and more flexible usage tracking (e.g., per-agent or per-task).

Comment on lines +466 to +481
def parse_messages(self, messages: List[Message]) -> List[Message]:
new_messages = []
for msg in messages:
role = getattr(msg, 'role', None)
content = getattr(msg, 'content', None)

if 'system' not in self.ignore_roles and role == 'system':
new_messages.append(msg)
if role == 'user':
new_messages.append(msg)
if 'assistant' not in self.ignore_roles and role == 'assistant' and content is not None:
new_messages.append(msg)
if 'tool' not in self.ignore_roles and role == 'tool':
new_messages.append(msg)

return new_messages

Choose a reason for hiding this comment

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

medium

The parse_messages method contains a series of if statements that can be simplified to improve readability and reduce repetition. Consolidating the logic for checking roles against self.ignore_roles would make the function's intent clearer and easier to maintain.

    def parse_messages(self, messages: List[Message]) -> List[Message]:
        new_messages = []
        for msg in messages:
            role = getattr(msg, 'role', None)

            if role == 'user':
                new_messages.append(msg)
                continue

            if role in self.ignore_roles:
                continue

            if role == 'assistant' and getattr(msg, 'content', None) is None:
                continue

            new_messages.append(msg)

        return new_messages

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.

1 participant