Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • VR greenhouse system offers remote farm walking
    • UK-based Circular11 secures €2.7 million to turn low-grade plastic waste into building materials
    • Anthropic Confidentially Files for What Could Be the Largest IPO Ever
    • Salesforce has a stake in Anthropic worth ~$5B; Salesforce first invested about $50M in an early 2023 round and has continually invested in rounds since (Brody Ford/Bloomberg)
    • Russia’s Military Hackers Targeted Home Routers Across 23 States. Here’s What to Do
    • How to Combine Claude Code and Codex for Maximum Coding Power
    • Supermassive black holes may create millions of new planets
    • Cheque in: 3 startups ended May by raising $15.5 million
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Monday, June 1
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»From Data to Stories: Code Agents for KPI Narratives
    Artificial Intelligence

    From Data to Stories: Code Agents for KPI Narratives

    Editor Times FeaturedBy Editor Times FeaturedMay 29, 2025No Comments30 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    , we regularly want to analyze what’s occurring with KPIs: whether or not we’re reacting to anomalies on our dashboards or simply routinely doing a numbers replace. Primarily based on my years of expertise as a KPI analyst, I might estimate that greater than 80% of those duties are pretty normal and might be solved simply by following a easy guidelines. 

    Right here’s a high-level plan for investigating a KPI change (you’ll find extra particulars within the article “Anomaly Root Cause Analysis 101”):

    • Estimate the top-line change within the metric to grasp the magnitude of the shift. 
    • Verify information high quality to make sure that the numbers are correct and dependable.
    • Collect context about inner and exterior occasions that may have influenced the change.
    • Slice and cube the metric to determine which segments are contributing to the metric’s shift.
    • Consolidate your findings in an govt abstract that features hypotheses and estimates of their impacts on the principle KPI.

    Since now we have a transparent plan to execute, such duties can probably be automated utilizing AI brokers. The code brokers we just lately discussed could possibly be match there, as their potential to write down and execute code will assist them to analyse information effectively, with minimal back-and-forth. So, let’s attempt constructing such an agent utilizing the HuggingFace smolagents framework. 

    Whereas engaged on our process, we’ll talk about extra superior options of the smolagents framework:

    • Methods for tweaking all types of prompts to make sure the specified behaviour.
    • Constructing a multi-agent system that may clarify the Kpi adjustments and hyperlink them to root causes. 
    • Including reflection to the circulation with supplementary planning steps.

    MVP for explaining KPI adjustments

    As ordinary, we’ll take an iterative method and begin with a easy MVP, specializing in the slicing and dicing step of the evaluation. We are going to analyse the adjustments of a easy metric (income) cut up by one dimension (nation). We are going to use the dataset from my earlier article, “Making sense of KPI changes”.

    Let’s load the information first. 

    raw_df = pd.read_csv('absolute_metrics_example.csv', sep = 't')
    df = raw_df.groupby('nation')[['revenue_before', 'revenue_after_scenario_2']].sum()
      .sort_values('revenue_before', ascending = False).rename(
        columns = {'revenue_after_scenario_2': 'after', 
          'revenue_before': 'earlier than'})
    Picture by writer

    Subsequent, let’s initialise the mannequin. I’ve chosen the OpenAI GPT-4o-mini as my most well-liked possibility for easy duties. Nonetheless, the smolagents framework supports all types of fashions, so you should use the mannequin you favor. Then, we simply have to create an agent and provides it the duty and the dataset.

    from smolagents import CodeAgent, LiteLLMModel
    
    mannequin = LiteLLMModel(model_id="openai/gpt-4o-mini", 
      api_key=config['OPENAI_API_KEY']) 
    
    agent = CodeAgent(
        mannequin=mannequin, instruments=[], max_steps=10,
        additional_authorized_imports=["pandas", "numpy", "matplotlib.*", 
          "plotly.*"], verbosity_level=1 
    )
    
    process = """
    Here's a dataframe displaying income by section, evaluating values 
    earlier than and after.
    May you please assist me perceive the adjustments? Particularly:
    1. Estimate how the full income and the income for every section 
    have modified, each in absolute phrases and as a proportion.
    2. Calculate the contribution of every section to the full 
    change in income.
    
    Please spherical all floating-point numbers within the output 
    to 2 decimal locations.
    """
    
    agent.run(
        process,
        additional_args={"information": df},
    )

    The agent returned fairly a believable consequence. We obtained detailed statistics on the metric adjustments in every section and their affect on the top-line KPI.

    {'total_before': 1731985.21, 'total_after': 
    1599065.55, 'total_change': -132919.66, 'segment_changes': 
    {'absolute_change': {'different': 4233.09, 'UK': -4376.25, 'France': 
    -132847.57, 'Germany': -690.99, 'Italy': 979.15, 'Spain': 
    -217.09}, 'percentage_change': {'different': 0.67, 'UK': -0.91, 
    'France': -55.19, 'Germany': -0.43, 'Italy': 0.81, 'Spain': 
    -0.23}, 'contribution_to_change': {'different': -3.18, 'UK': 3.29, 
    'France': 99.95, 'Germany': 0.52, 'Italy': -0.74, 'Spain': 0.16}}}

    Let’s check out the code generated by the agent. It’s advantageous, however there’s one potential situation. The Llm recreated the dataframe primarily based on the enter information as an alternative of referencing it instantly. This method is just not splendid (particularly when working with huge datasets), as it might probably result in errors and better token utilization.

    import pandas as pd                                                                                                        
     
    # Creating the DataFrame from the supplied information                 
    information = {                                                        
        'earlier than': [632767.39, 481409.27, 240704.63, 160469.75,      
    120352.31, 96281.86],                                           
        'after': [637000.48, 477033.02, 107857.06, 159778.76,       
    121331.46, 96064.77]                                            
    }                                                               
    index = ['other', 'UK', 'France', 'Germany', 'Italy', 'Spain']  
    df = pd.DataFrame(information, index=index)                            
                                                                    
    # Calculating whole income earlier than and after                    
    total_before = df['before'].sum()                               
    total_after = df['after'].sum()                                 
                                                                    
    # Calculating absolute and proportion change for every section   
    df['absolute_change'] = df['after'] - df['before']              
    df['percentage_change'] = (df['absolute_change'] /              
    df['before']) * 100                                             
                                                                    
    # Calculating whole income change                              
    total_change = total_after - total_before                       
                                                                    
    # Calculating contribution of every section to the full change  
    df['contribution_to_change'] = (df['absolute_change'] /         
    total_change) * 100                                             
                                                                    
    # Rounding outcomes                                              
    df = df.spherical(2)                                                
                                                                    
    # Printing the calculated outcomes                               
    print("Whole income earlier than:", total_before)                    
    print("Whole income after:", total_after)                      
    print("Whole change in income:", total_change)                 
    print(df)

    It’s price fixing this drawback earlier than shifting on to constructing a extra advanced system.

    Tweaking prompts

    For the reason that LLM is simply following the directions given to it, we’ll tackle this situation by tweaking the immediate.

    Initially, I tried to make the duty immediate extra specific, clearly instructing the LLM to make use of the supplied variable.

    process = """Here's a dataframe displaying income by section, evaluating 
    values earlier than and after. The info is saved in df variable. 
    Please, use it and do not attempt to parse the information your self. 
    
    May you please assist me perceive the adjustments?
    Particularly:
    1. Estimate how the full income and the income for every section 
    have modified, each in absolute phrases and as a proportion.
    2. Calculate the contribution of every section to the full change in income.
    
    Please spherical all floating-point numbers within the output to 2 decimal locations.
    """

    It didn’t work. So, the following step is to look at the system immediate and see why it really works this manner. 

    print(agent.prompt_templates['system_prompt'])
    
    #... 
    # Listed here are the foundations it's best to at all times observe to unravel your process:
    # 1. At all times present a 'Thought:' sequence, and a 'Code:n```py' sequence ending with '```' sequence, else you'll fail.
    # 2. Use solely variables that you've outlined.
    # 3. At all times use the precise arguments for the instruments. DO NOT cross the arguments as a dict as in 'reply = wiki({'question': "What's the place the place James Bond lives?"})', however use the arguments instantly as in 'reply = wiki(question="What's the place the place James Bond lives?")'.
    # 4. Take care to not chain too many sequential device calls in the identical code block, particularly when the output format is unpredictable. For example, a name to look has an unpredictable return format, so wouldn't have one other device name that relies on its output in the identical block: moderately output outcomes with print() to make use of them within the subsequent block.
    # 5. Name a device solely when wanted, and by no means re-do a device name that you simply beforehand did with the very same parameters.
    # 6. Do not title any new variable with the identical title as a device: as an example do not title a variable 'final_answer'.
    # 7. By no means create any notional variables in our code, as having these in your logs will derail you from the true variables.
    # 8. You need to use imports in your code, however solely from the next listing of modules: ['collections', 'datetime', 'itertools', 'math', 'numpy', 'pandas', 'queue', 'random', 're', 'stat', 'statistics', 'time', 'unicodedata']
    # 9. The state persists between code executions: so if in a single step you have created variables or imported modules, these will all persist.
    # 10. Do not quit! You are in control of fixing the duty, not offering instructions to unravel it.
    # Now Start!

    On the finish of the immediate, now we have the instruction "# 2. Use solely variables that you've outlined!". This could be interpreted as a strict rule to not use some other variables. So, I modified it to "# 2. Use solely variables that you've outlined or ones supplied in further arguments! By no means attempt to copy and parse further arguments." 

    modified_system_prompt = agent.prompt_templates['system_prompt']
        .change(
            '2. Use solely variables that you've outlined!', 
            '2. Use solely variables that you've outlined or ones supplied in further arguments! By no means attempt to copy and parse further arguments.'
        )
    agent.prompt_templates['system_prompt'] = modified_system_prompt

    This transformation alone didn’t assist both. Then, I examined the duty message. 

    ╭─────────────────────────── New run ────────────────────────────╮
    │                                                                │
    │ Here's a pandas dataframe displaying income by section,         │
    │ evaluating values earlier than and after.                             │
    │ May you please assist me perceive the adjustments?               │
    │ Particularly:                                                  │
    │ 1. Estimate how the full income and the income for every     │
    │ section have modified, each in absolute phrases and as a          │
    │ proportion.                                                    │
    │ 2. Calculate the contribution of every section to the full     │
    │ change in income.                                             │
    │                                                                │
    │ Please spherical all floating-point numbers within the output to 2   │
    │ decimal locations.                                                │
    │                                                                │
    │ You've been supplied with these further arguments, that   │
    │ you possibly can entry utilizing the keys as variables in your python      │
    │ code:                                                          │
    │ {'df':             earlier than      after                           │
    │ nation                                                        │
    │ different    632767.39  637000.48                                  │
    │ UK       481409.27  477033.02                                  │
    │ France   240704.63  107857.06                                  │
    │ Germany  160469.75  159778.76                                  │
    │ Italy    120352.31  121331.46                                  │
    │ Spain     96281.86   96064.77}.                                │
    │                                                                │
    ╰─ LiteLLMModel - openai/gpt-4o-mini ────────────────────────────╯

    It has an instruction associated the the utilization of further arguments "You've been supplied with these further arguments, you could entry utilizing the keys as variables in your python code". We are able to attempt to make it extra particular and clear. Sadly, this parameter is just not uncovered externally, so I needed to find it in the source code. To seek out the trail of a Python bundle, we are able to use the next code.

    import smolagents 
    print(smolagents.__path__)

    Then, I discovered the brokers.py file and modified this line to incorporate a extra particular instruction.

    self.process += f"""
    You've been supplied with these further arguments out there as variables 
    with names {",".be a part of(additional_args.keys())}. You may entry them instantly. 
    Here's what they comprise (only for informational functions): 
    {str(additional_args)}."""

    It was a little bit of hacking, however that’s typically what occurs with the LLM frameworks. Don’t neglect to reload the bundle afterwards, and we’re good to go. Let’s check whether or not it really works now.

    process = """
    Here's a pandas dataframe displaying income by section, evaluating values 
    earlier than and after. 
    
    Your process shall be perceive the adjustments to the income (after vs earlier than) 
    in numerous segments and supply govt abstract.
    Please, observe the next steps:
    1. Estimate how the full income and the income for every section 
    have modified, each in absolute phrases and as a proportion.
    2. Calculate the contribution of every section to the full change 
    in income.
    
    Spherical all floating-point numbers within the output to 2 decimal locations. 
    """
    agent.logger.degree = 1 # Decrease verbosity degree
    agent.run(
        process,
        additional_args={"df": df},
    )

    Hooray! The issue has been mounted. The agent now not copies the enter variables and references df variable instantly as an alternative. Right here’s the newly generated code.

    import pandas as pd                                             
                                                                      
    # Calculate whole income earlier than and after                      
    total_before = df['before'].sum()                               
    total_after = df['after'].sum()                                 
    total_change = total_after - total_before                       
    percentage_change_total = (total_change / total_before * 100)   
    if total_before != 0 else 0                                     
                                                                    
    # Spherical values                                                  
    total_before = spherical(total_before, 2)                           
    total_after = spherical(total_after, 2)                             
    total_change = spherical(total_change, 2)                           
    percentage_change_total = spherical(percentage_change_total, 2)     
                                                                    
    # Show outcomes                                               
    print(f"Whole Income Earlier than: {total_before}")                  
    print(f"Whole Income After: {total_after}")                    
    print(f"Whole Change: {total_change}")                          
    print(f"Share Change: {percentage_change_total}%")

    Now, we’re prepared to maneuver on to constructing the precise agent that can clear up our process.

    AI agent for KPI narratives

    Lastly, it’s time to work on the AI agent that can assist us clarify KPI adjustments and create an govt abstract.

    Our agent will observe this plan for the basis trigger evaluation:

    • Estimate the top-line KPI change. 
    • Slice and cube the metric to grasp which segments are driving the shift. 
    • Lookup occasions within the change log to see whether or not they can clarify the metric adjustments.
    • Consolidate all of the findings within the complete govt abstract.

    After a whole lot of experimentation and a number of other tweaks, I’ve arrived at a promising consequence. Listed here are the important thing changes I made (we’ll talk about them intimately later):

    • I leveraged the multi-agent setup by including one other workforce member — the change log Agent, who can entry the change log and help in explaining KPI adjustments.
    • I experimented with extra highly effective fashions like gpt-4o and gpt-4.1-mini since gpt-4o-mini wasn’t enough. Utilizing stronger fashions not solely improved the outcomes, but in addition considerably lowered the variety of steps: with gpt-4.1-miniI obtained the ultimate consequence after simply six steps, in comparison with 14–16 steps with gpt-4o-mini. This implies that investing in costlier fashions could be worthwhile for agentic workflows.
    • I supplied the agent with the advanced device to analyse KPI adjustments for easy metrics. The device performs all of the calculations, whereas LLM can simply interpret the outcomes. I mentioned the method to KPI adjustments evaluation intimately in my previous article. 
    • I reformulated the immediate into a really clear step-by-step information to assist the agent keep on observe. 
    • I added planning steps that encourage the LLM agent to assume by means of its method first and revisit the plan each three iterations. 

    After all of the changes, I obtained the next abstract from the agent, which is fairly good.

    Govt Abstract:
    Between April 2025 and Could 2025, whole income declined sharply by
    roughly 36.03%, falling from 1,731,985.21 to 1,107,924.43, a
    drop of -624,060.78 in absolute phrases.
    This decline was primarily pushed by vital income 
    reductions within the 'new' buyer segments throughout a number of 
    international locations, with declines of roughly 70% in these segments.
    
    Probably the most impacted segments embrace:
    - other_new: earlier than=233,958.42, after=72,666.89, 
    abs_change=-161,291.53, rel_change=-68.94%, share_before=13.51%, 
    affect=25.85, impact_norm=1.91
    - UK_new: earlier than=128,324.22, after=34,838.87, 
    abs_change=-93,485.35, rel_change=-72.85%, share_before=7.41%, 
    affect=14.98, impact_norm=2.02
    - France_new: earlier than=57,901.91, after=17,443.06, 
    abs_change=-40,458.85, rel_change=-69.87%, share_before=3.34%, 
    affect=6.48, impact_norm=1.94
    - Germany_new: earlier than=48,105.83, after=13,678.94, 
    abs_change=-34,426.89, rel_change=-71.56%, share_before=2.78%, 
    affect=5.52, impact_norm=1.99
    - Italy_new: earlier than=36,941.57, after=11,615.29, 
    abs_change=-25,326.28, rel_change=-68.56%, share_before=2.13%, 
    affect=4.06, impact_norm=1.91
    - Spain_new: earlier than=32,394.10, after=7,758.90, 
    abs_change=-24,635.20, rel_change=-76.05%, share_before=1.87%, 
    affect=3.95, impact_norm=2.11
    
    Primarily based on evaluation from the change log, the principle causes for this 
    development are:
    1. The introduction of recent onboarding controls applied on Could 
    8, 2025, which lowered new buyer acquisition by about 70% to 
    forestall fraud.
    2. A postal service strike within the UK beginning April 5, 2025, 
    inflicting order supply delays and elevated cancellations 
    impacting the UK new section.
    3. A rise in VAT by 2% in Spain as of April 22, 2025, 
    affecting new buyer pricing and inflicting increased cart 
    abandonment.
    
    These components mixed clarify the outsized destructive impacts 
    noticed in new buyer segments and the general income decline.

    The LLM agent additionally generated a bunch of illustrative charts (they had been a part of our progress explaining device). For instance, this one reveals the impacts throughout the mix of nation and maturity.

    Picture by writer

    The outcomes look actually thrilling. Now let’s dive deeper into the precise implementation to grasp the way it works beneath the hood. 

    Multi-AI agent setup

    We are going to begin with our change log agent. This agent will question the change log and attempt to determine potential root causes for the metric adjustments we observe. Since this agent doesn’t have to do advanced operations, we implement it as a ToolCallingAgent. As a result of this agent shall be known as by one other agent, we have to outline its title and description attributes.

    @device 
    def get_change_log(month: str) -> str: 
        """
        Returns the change log (listing of inner and exterior occasions that may have affected our KPIs) for the given month 
    
        Args:
            month: month within the format %Y-%m-01, for instance, 2025-04-01
        """
        return events_df[events_df.month == month].drop('month', axis = 1).to_dict('data')
    
    mannequin = LiteLLMModel(model_id="openai/gpt-4.1-mini", api_key=config['OPENAI_API_KEY'])
    change_log_agent = ToolCallingAgent(
        instruments=[get_change_log],
        mannequin=mannequin,
        max_steps=10,
        title="change_log_agent",
        description="Helps you discover the related info within the change log that may clarify adjustments on metrics. Present the agent with all of the context to obtain information",
    )

    For the reason that supervisor agent shall be calling this agent, we gained’t have any management over the question it receives. Subsequently, I made a decision to switch the system immediate to incorporate further context.

    change_log_system_prompt = '''
    You are a grasp of the change log and also you assist others to elucidate 
    the adjustments to metrics. If you obtain a request, lookup the listing of occasions 
    occurred by month, then filter the related info primarily based 
    on supplied context and return again. Prioritise probably the most possible components 
    affecting the KPI and restrict your reply solely to them.
    '''
    
    modified_system_prompt = change_log_agent.prompt_templates['system_prompt'] 
      + 'nnn' + change_log_system_prompt
    
    change_log_agent.prompt_templates['system_prompt'] = modified_system_prompt

    To allow the first agent to delegate duties to the change log agent, we merely have to specify it within the managed_agents discipline.

    agent = CodeAgent(
        mannequin=mannequin,
        instruments=[calculate_simple_growth_metrics],
        max_steps=20,
        additional_authorized_imports=["pandas", "numpy", "matplotlib.*", "plotly.*"],
        verbosity_level = 2, 
        planning_interval = 3,
        managed_agents = [change_log_agent]
    )

    Let’s see the way it works. First, we are able to have a look at the brand new system immediate for the first agent. It now consists of details about workforce members and directions on tips on how to ask them for assist.

    You too can give duties to workforce members.
    Calling a workforce member works the identical as for calling a device: merely, 
    the one argument you can provide within the name is 'process'.
    Provided that this workforce member is an actual human, you have to be very verbose 
    in your process, it ought to be an extended string offering informations 
    as detailed as vital.
    Here's a listing of the workforce members you could name:
    ```python
    def change_log_agent("Your question goes right here.") -> str:
        """Helps you discover the related info within the change log that 
        can clarify adjustments on metrics. Present the agent with all of the context 
        to obtain information"""
    ```

    The execution log reveals that the first agent efficiently delegated the duty to the second agent and obtained the next response.

    <-- Major agent calling the change log agent -->
    
    ─ Executing parsed code: ─────────────────────────────────────── 
      # Question change_log_agent with the detailed process description     
      ready                                                        
      context_for_change_log = (                                      
          "We analyzed adjustments in income from April 2025 to Could      
      2025. We discovered giant decreases "                                
          "primarily within the 'new' maturity segments throughout international locations:    
      Spain_new, UK_new, Germany_new, France_new, Italy_new, and      
      other_new. "                                                    
          "The income fell by round 70% in these segments, which    
      have outsized destructive affect on whole income change. "        
          "We wish to know the 1-3 most possible causes for this      
      vital drop in income within the 'new' buyer segments      
      throughout this era."                                            
      )                                                               
                                                                      
      clarification = change_log_agent(process=context_for_change_log)     
      print("Change log agent clarification:")                          
      print(clarification)                                              
     ──────────────────────────────────────────────────────────────── 
    
    <-- Change log agent execution begin -->
    ╭──────────────────── New run - change_log_agent ─────────────────────╮
    │                                                                     │
    │ You are a useful agent named 'change_log_agent'.                    │
    │ You've been submitted this process by your supervisor.                  │
    │ ---                                                                 │
    │ Process:                                                               │
    │ We analyzed adjustments in income from April 2025 to Could 2025.         │
    │ We discovered giant decreases primarily within the 'new' maturity segments      │
    │ throughout international locations: Spain_new, UK_new, Germany_new, France_new,       │
    │ Italy_new, and other_new. The income fell by round 70% in these   │
    │ segments, which have outsized destructive affect on whole income      │
    │ change. We wish to know the 1-3 most possible causes for this       │
    │ vital drop in income within the 'new' buyer segments throughout   │
    │ this era.                                                        │
    │ ---                                                                 │
    │ You are serving to your supervisor clear up a wider process: so make certain to     │
    │ not present a one-line reply, however give as a lot info as      │
    │ attainable to provide them a transparent understanding of the reply.          │
    │                                                                     │
    │ Your final_answer WILL HAVE to comprise these components:                 │
    │ ### 1. Process final result (brief model):                                │
    │ ### 2. Process final result (extraordinarily detailed model):                   │
    │ ### 3. Further context (if related):                            │
    │                                                                     │
    │ Put all these in your final_answer device, every part that you simply do     │
    │ not cross as an argument to final_answer shall be misplaced.               │
    │ And even when your process decision is just not profitable, please return   │
    │ as a lot context as attainable, in order that your supervisor can act upon      │
    │ this suggestions.                                                      │
    │                                                                     │
    ╰─ LiteLLMModel - openai/gpt-4.1-mini ────────────────────────────────╯

    Utilizing the smolagents framework, we are able to simply arrange a easy multi-agent system, the place a supervisor agent coordinates and delegates duties to workforce members with particular abilities. 

    Iterating on the immediate

    We’ve began with a really high-level immediate outlining the purpose and a obscure course, however sadly, it didn’t work persistently. LLMs should not sensible sufficient but to determine the method on their very own. So, I created an in depth step-by-step immediate describing the entire plan and together with the detailed specs of the expansion narrative device we’re utilizing. 

    process = """
    Here's a pandas dataframe displaying the income by section, evaluating values 
    earlier than (April 2025) and after (Could 2025). 
    
    You are a senior and skilled information analyst. Your process shall be to grasp 
    the adjustments to the income (after vs earlier than) in numerous segments 
    and supply govt abstract.
    
    ## Observe the plan:
    1. Begin by udentifying the listing of dimensions (columns in dataframe that 
    should not "earlier than" and "after")
    2. There could be a number of dimensions within the dataframe. Begin high-level 
    by taking a look at every dimension in isolation, mix all outcomes 
    collectively into the listing of segments analysed (remember to avoid wasting 
    the dimension used for every section). 
    Use the supplied instruments to analyse the adjustments of metrics: {tools_description}. 
    3. Analyse the outcomes from earlier step and hold solely segments 
    which have outsized affect on the KPI change (absolute of impact_norm 
    is above 1.25). 
    4. Verify what dimensions are current within the listing of serious section, 
    if there are a number of ones - execute the device on their mixtures 
    and add to the analysed segments. If after including an extra dimension, 
    all subsegments present shut different_rate and impact_norm values, 
    then we are able to exclude this cut up (though impact_norm is above 1.25), 
    because it would not clarify something. 
    5. Summarise the numerous adjustments you recognized. 
    6. Attempt to clarify what's going on with metrics by getting information 
    from the change_log_agent. Please, present the agent the complete context 
    (what segments have outsized affect, what's the relative change and 
    what's the interval we're taking a look at). 
    Summarise the knowledge from the changelog and point out 
    solely 1-3 probably the most possible causes of the KPI change 
    (ranging from probably the most impactful one).
    7. Put collectively 3-5 sentences commentary what occurred high-level 
    and why (primarily based on the data obtained from the change log). 
    Then observe it up with extra detailed abstract: 
    - Prime-line whole worth of metric earlier than and after in human-readable format, 
    absolute and relative change 
    - Listing of segments that meaningfully influenced the metric positively 
    or negatively with the next numbers: values earlier than and after, 
    absoltue and relative change, share of section earlier than, affect 
    and normed affect. Order the segments by absolute worth 
    of absolute change because it represents the ability of affect. 
    
    ## Instruction on the calculate_simple_growth_metrics device:
    By default, it's best to use the device for the entire dataset not the section, 
    because it gives you the complete details about the adjustments.
    
    Right here is the steerage tips on how to interpret the output of the device
    - distinction - absolutely the distinction between after and earlier than values
    - difference_rate - the relative distinction (if it is shut for 
      all segments then the dimension is just not informative)
    - affect - the share of KPI differnce defined by this section 
    - segment_share_before - share of section earlier than
    - impact_norm - affect normed on the share of segments, we're  
      in very excessive or very low numbers since they present outsized affect, 
      rule of thumb - impact_norm between -1.25 and 1.25 is not-informative 
    
    When you're utilizing the device on the subset of dataframe remember, 
    that the outcomes will not be aplicable to the complete dataset, so keep away from utilizing it 
    until you wish to explicitly have a look at subset (i.e. change in France). 
    When you determined to make use of the device on a selected section 
    and share these leads to the chief abstract, explicitly define 
    that we're diving deeper into a selected section.
    """.format(tools_description = tools_description)
    agent.run(
        process,
        additional_args={"df": df},
    )

    Explaining every part in such element was fairly a frightening process, nevertheless it’s vital if we wish constant outcomes.

    Planning steps

    The smolagents framework allows you to add planning steps to your agentic circulation. This encourages the agent to start out with a plan and replace it after the required variety of steps. From my expertise, this reflection could be very useful for sustaining concentrate on the issue and adjusting actions to remain aligned with the preliminary plan and purpose. I undoubtedly advocate utilizing it in instances when advanced reasoning is required.

    Setting it up is as straightforward as specifying planning_interval = 3 for the code agent.

    agent = CodeAgent(
        mannequin=mannequin,
        instruments=[calculate_simple_growth_metrics],
        max_steps=20,
        additional_authorized_imports=["pandas", "numpy", "matplotlib.*", "plotly.*"],
        verbosity_level = 2, 
        planning_interval = 3,
        managed_agents = [change_log_agent]
    )

    That’s it. Then, the agent offers reflections beginning with excited about the preliminary plan.

    ────────────────────────── Preliminary plan ──────────────────────────
    Listed here are the info I do know and the plan of motion that I'll 
    observe to unravel the duty:
    ```
    ## 1. Info survey
    
    ### 1.1. Info given within the process
    - We have now a pandas dataframe `df` displaying income by section, for 
    two time factors: earlier than (April 2025) and after (Could 2025).
    - The dataframe columns embrace:
      - Dimensions: `nation`, `maturity`, `country_maturity`, 
    `country_maturity_combined`
      - Metrics: `earlier than` (income in April 2025), `after` (income in
    Could 2025)
    - The duty is to grasp the adjustments in income (after vs 
    earlier than) throughout totally different segments.
    - Key directions and instruments supplied:
      - Establish all dimensions besides earlier than/after for segmentation.
      - Analyze every dimension independently utilizing 
    `calculate_simple_growth_metrics`.
      - Filter segments with outsized affect on KPI change (absolute 
    normed affect > 1.25).
      - Look at mixtures of dimensions if a number of dimensions have
    vital segments.
      - Summarize vital adjustments and have interaction `change_log_agent` 
    for contextual causes.
      - Present a ultimate govt abstract together with top-line adjustments 
    and segment-level detailed impacts.
    - Dataset snippet reveals segments combining international locations (`France`, 
    `UK`, `Germany`, `Italy`, `Spain`, `different`) and maturity standing 
    (`new`, `present`).
    - The mixed segments are uniquely recognized in columns 
    `country_maturity` and `country_maturity_combined`.
    
    ### 1.2. Info to lookup
    - Definitions or descriptions of the segments if unclear (e.g., 
    what defines `new` vs `present` maturity).
      - Seemingly not necessary to proceed, however could possibly be requested from 
    enterprise documentation or change log.
    - Extra particulars on the change log (accessible through 
    `change_log_agent`) that would present possible causes for income
    adjustments.
    - Affirmation on dealing with mixed dimension splits - how precisely
    `country_maturity_combined` is shaped and ought to be interpreted in
    mixed dimension evaluation.
    - Knowledge dictionary or description of metrics if any further KPI 
    moreover income is related (unlikely given information).
    - Dates verify interval of research: April 2025 (earlier than) and Could 
    2025 (after). No have to look these up since given.
    
    ### 1.3. Info to derive
    - Establish all dimension columns out there for segmentation:
      - By excluding 'earlier than' and 'after', seemingly candidates are 
    `nation`, `maturity`, `country_maturity`, and 
    `country_maturity_combined`.
    - For every dimension, calculate change metrics utilizing the given 
    device:
      - Absolute and relative distinction in income per section.
      - Influence, section share earlier than, and normed affect for every 
    section.
    - Establish which segments have outsized affect on KPI change 
    (|impact_norm| > 1.25).
    - If a number of dimensions have vital segments, mix 
    dimensions (e.g., nation + maturity) and reanalyze.
    - Decide if mixed dimension splits present significant 
    differentiation or not, primarily based on delta charge and impact_norm 
    consistency.
    - Summarize course and magnitude of KPI adjustments at top-line 
    degree (combination income earlier than and after).
    - Establish prime segments driving constructive and destructive adjustments 
    primarily based on ordered absolute absolute_change.
    - Collect contextual insights from the change log agent concerning 
    possible causes tied to vital segments and the Could 2025 vs 
    April 2025 interval.
    
    ## 2. Plan
    
    1. Establish all dimension columns current within the dataframe by 
    itemizing columns and excluding 'earlier than' and 'after'.
    2. For every dimension recognized (`nation`, `maturity`, 
    `country_maturity`, `country_maturity_combined`):
       - Use `calculate_simple_growth_metrics` on the complete dataframe 
    grouped by that dimension.
       - Extract segments with calculated metrics together with 
    impact_norm.
    3. Mixture outcomes from all single-dimension analyses and filter
    segments the place |impact_norm| > 1.25.
    4. Decide which dimensions these vital segments belong 
    to.
    5. If multiple dimension is represented in these vital 
    segments, analyze the mixed dimension shaped by these 
    dimensions (for instance, mixture of `nation` and `maturity` 
    or use present mixed dimension columns).
    6. Repeat metric calculation utilizing 
    `calculate_simple_growth_metrics` on the mixed dimension.
    7. Look at if the mixed dimension splits create significant 
    differentiation - if all subsegments present shut difference_rate 
    and impact_norm, exclude the cut up.
    8. Put together a abstract of serious adjustments:
       - Prime-line KPIs earlier than and after (absolute and relative 
    adjustments).
       - Listing of impactful segments sorted by absolute absolute_change
    that influenced general income.
    9. Present the listing of segments with particulars (values earlier than, 
    after, absolute and relative change, share earlier than, affect, 
    impact_norm).
    10. Utilizing this summarized info, question `change_log_agent` 
    with full context:
        - Embody vital segments, their relative adjustments, and 
    intervals (April to Could 2025).
    11. Course of the agent's response to determine 1-3 foremost possible 
    causes of the KPI adjustments.
    12. Draft govt abstract commentary:
        - Excessive-level overview of what occurred and why, primarily based on log 
    information.
        - Detailed abstract together with top-line adjustments and 
    segment-level metrics affect.
    13. Ship the ultimate reply utilizing `final_answer` device containing 
    the above govt abstract and data-driven insights.

    Then, after every three steps, the agent revisits and updates the plan. 

    ────────────────────────── Up to date plan ──────────────────────────
    I nonetheless want to unravel the duty I used to be given:
    ```
    
    Here's a pandas dataframe displaying the income by section, 
    evaluating values earlier than (April 2025) and after (Could 2025). 
    
    You are a senior and skilled information analyst. Your process shall be 
    perceive the adjustments to the income (after vs earlier than) in 
    totally different segments 
    and supply govt abstract.
    
    <... repeating the complete preliminary process ...>
    ```
    
    Listed here are the info I do know and my new/up to date plan of motion to 
    clear up the duty:
    ```
    ## 1. Up to date info survey
    
    ### 1.1. Info given within the process
    - We have now a pandas dataframe with income by section, displaying 
    values "earlier than" (April 2025) and "after" (Could 2025).
    - Columns within the dataframe embrace a number of dimensions and the 
    "earlier than" and "after" income values.
    - The purpose is to grasp income adjustments by section and supply
    an govt abstract.
    - Steering and guidelines about tips on how to analyze and interpret outcomes 
    from the `calculate_simple_growth_metrics` device are supplied.
    - The dataframe accommodates columns: nation, maturity, 
    country_maturity, country_maturity_combined, earlier than, after.
    
    ### 1.2. Info that now we have realized
    - The size to investigate are: nation, maturity, 
    country_maturity, and country_maturity_combined.
    - Analyzed income adjustments by dimension.
    - Solely the "new" maturity section has vital affect 
    (impact_norm=1.96 > 1.25), with a big destructive income change (~
    -70.6%).
    - Within the mixed section "country_maturity," the "new" segments 
    throughout international locations (Spain_new, UK_new, Germany_new, France_new, 
    Italy_new, other_new) all have outsized destructive impacts with 
    impact_norm values all above 1.9.
    - The mature/present segments in these international locations have smaller 
    normed impacts under 1.25.
    - Nation-level and maturity-level section dimension alone are 
    much less revealing than the mixed nation+maturity section 
    dimension which highlights the brand new segments as strongly impactful.
    - Whole income dropped considerably from earlier than to after, principally
    pushed by new segments shrinking drastically.
    
    ### 1.3. Info nonetheless to lookup
    - Whether or not splitting the information by further dimensions past 
    nation and maturity (e.g., country_maturity_combined) explains 
    additional heterogeneous impacts or if the sample is uniform.
    - Clarification/context from change log about what precipitated the key 
    drop predominantly in new segments in all international locations.
    - Confirming whether or not any nation inside the new section behaved 
    in another way or mitigated losses.
    
    ### 1.4. Info nonetheless to derive
    - A concise govt abstract describing the top-level income 
    change and figuring out which segments clarify the declines.
    - Clarification involving the change log agent with abstract of 
    possible causes for these outsized reductions in income within the 
    new segments throughout international locations for April-Could 2025.
    
    ## 2. Plan
    
    ### 2.1. Confirm if including the extra dimension 
    'country_maturity_combined' splits the impactful "new" segments 
    into subsegments with considerably totally different impacts or if the 
    change charges and normed impacts are comparatively homogeneous. If 
    homogeneous, we don't achieve deeper perception and may disregard 
    additional splitting.
    
    ### 2.2. Summarize all vital segments recognized with 
    outsized impact_norm ≥ 1.25, together with their earlier than and after 
    values, absolute and relative adjustments, section shares earlier than, 
    affect, and normalized affect, ordered by absolute worth of the 
    change.
    
    ### 2.3. Question the change_log_agent with the complete context: 
    vital segments are the brand new country_maturity segments with 
    giant destructive adjustments (~ -70%), timeframe April 2025 to Could 2025,
    and request prime 1-3 most possible causes for the KPI income drop 
    in these segments.
    
    ### 2.4. Primarily based on the change log agent's response, synthesize a 
    3-5 sentence high-level commentary explaining what occurred 
    broadly and why.
    
    ### 2.5. Draft an in depth govt abstract together with:
    - Whole income earlier than and after in human-readable format with 
    absolute and relative change.
    - An inventory of serious segments driving these adjustments, so as 
    by absolute affect, with detailed numbers (earlier than, after, absolute
    and relative change, section share earlier than, affect, normed affect).
    
    ### 2.6. Use the `final_answer` device to provide the finalized 
    govt abstract report.

    I actually like how the agent is inspired to reiterate on the preliminary process and keep centered on the principle drawback. Common reflection like that is useful in actual life as nicely, as groups typically get slowed down within the course of and lose sight of the why behind what they’re doing. It’s fairly cool to see managerial greatest practices being built-in into agentic frameworks.

    That’s it! We’ve constructed a code agent able to analysing KPI adjustments for easy metrics and explored all the important thing nuances of the method.

    You will discover the entire code and execution logs on GitHub.

    Abstract

    We’ve experimented so much with code brokers and at the moment are prepared to attract conclusions. For our experiments, we used the HuggingFace smolagents framework for code brokers — a really helpful toolset that provides: 

    • straightforward integration with totally different LLMs (from native fashions through Ollama to public suppliers like Anthropic or OpenAI),
    • excellent logging that makes it straightforward to grasp the entire thought means of the agent and debug points,
    • potential to construct advanced techniques leveraging multi-AI agent setups or planning options with out a lot effort.

    Whereas smolagents is at the moment my favorite agentic framework, it has its limitations: 

    • It might lack flexibility at occasions. For instance, I needed to modify the immediate instantly within the supply code to get the behaviour I needed.
    • It solely helps hierarchical multi-agent set-up (the place one supervisor can delegate duties to different brokers), however doesn’t cowl sequential workflow or consensual decision-making processes.
    • There’s no help for long-term reminiscence out of the field, that means you’re ranging from scratch with each process.

    Thank you numerous for studying this text. I hope this text was insightful for you.

    Reference

    This text is impressed by the “Building Code Agents with Hugging Face smolagents” brief course by DeepLearning.AI.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    How to Combine Claude Code and Codex for Maximum Coding Power

    June 1, 2026

    It’s the Lessons We Learned Along the Way. Or, Is It?

    June 1, 2026

    Proxy-Pointer RAG: Eliminating Wasteful Entity & Relations Extraction in Knowledge Graphs

    May 31, 2026

    Solving a Murder Mystery Using Bayesian Inference

    May 31, 2026

    Rerankers Aren’t Magic Either: When the Cross-Encoder Layer Is Worth the Cost

    May 31, 2026

    Qdrant TurboQuant Explained: Is TurboQuant the Silver Bullet?

    May 30, 2026

    Comments are closed.

    Editors Picks

    VR greenhouse system offers remote farm walking

    June 1, 2026

    UK-based Circular11 secures €2.7 million to turn low-grade plastic waste into building materials

    June 1, 2026

    Anthropic Confidentially Files for What Could Be the Largest IPO Ever

    June 1, 2026

    Salesforce has a stake in Anthropic worth ~$5B; Salesforce first invested about $50M in an early 2023 round and has continually invested in rounds since (Brody Ford/Bloomberg)

    June 1, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    The Top Tech Deals Our Readers Are Shopping During Amazon’s Big Spring Sale

    March 25, 2026

    Cambridge GaN Devices secures €30.5 million to drive global growth in power semiconductor industry

    February 19, 2025

    Thousands of consumer routers hacked by Russia’s military

    April 8, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.