How to access the "inputs" dict defined in "main.py" in methods of "crew.py"

Hello Friends:

Let’s say that this is in my main.py:

import os

def run():
    """Run the nice crew."""
    inputs = {'topic': "my topic", 'k2': "v2",
              "api_key": os.environ["ANTHROPIC_API_KEY"]}

    MyNiceCrew().crew().kickoff(inputs=inputs)
[ ... snip ... ]

and this is in my crew.py:

[ ... snip ... ]
from llms import get_ollama_llm, get_cloud_llm

@CrewBase
class MyNiceCrew():
  """My Nice crew"""

  agents_config = 'config/agents.yaml'
  tasks_config = 'config/tasks.yaml'

  @agent
  def my_nice_agent(self,) -> Agent:
      cloud_llm_config = {"model": "claude-3-7-sonnet-latest",
                          "api_key": inputs["ANTHROPIC_API_KEY"]}

      return Agent(config=self.agents_config['my_nice_agent'], 
                   llm=get_cloud_llm(config=cloud_llm_config))


   @before_kickoff
   def before_kickoff_function(self, inputs):
       pass
[ ... snip ... ]

Notice that I’m trying to pass an API KEY (or whatever else I might need) via main.py’s inputs dict.

So, within the MyNiceCrew.my_nice_agent() method above, how can I access that inputs dictionary?

Can I simply add inputs to the arguments as follows, just as it is done with the @before_kickoff method (above)?

  def my_nice_agent(self, inputs) -> Agent:
       [ ... snip ... ]

  @before_kickoff
     def before_kickoff_function(self, inputs):
         pass

If not, is there another mechanism to achieve parameter passing?

Thank you.

You can access your inputs in the crews @before_kickoff methods as you have done above.

What are you trying to achieve with this, maybe if your intent is clear people can offer you better alternatives.

1 Like

Thank you. It’s pretty much exactly as I wrote: I prefer introducing the API_KEY in main.py (a quite logical place to look), and then pass it to the Agent methods shown so I can pass it to the LLM class. I don’t want to bury them deeply from the start as it’s unintuitive.

Yeah you can pass them in as inputs as you described. Unless you want to use different API keys for kickoffs I think this approach introduces unnecessary overhead. It adds extra code to maintain (the @before_kickoff) methods. I’m not sure it’s worth it.

1 Like

Thank you. :blush: Indeed, I do wish to use different providers and their respective API KEYS. That’s because, apart from getting agents to work, I use observability & evaluation platforms (eg, Langfuse, Comet Opik, Arize AI Phoenix, and OpenLIT) to measure and record metrics.

By the way, I wouldn’t be accessing inputs via the @before_kickoff method (which I know works), but rather via @agent methods by also passing inputs as an argument there. This is what I needed clarification on.

I’m not sure I understand, can you show me pseudo code of how you intent to use the inputs?

From my understanding, the inputs are interpolated to the values in the tasks and agents .toml files. I haven’t seen them being used any other way :sweat_smile:

1 Like

I am aslo trying to access the inputs passed in the main.py file in the crew.py file, the @before_kickoff method is not working.

Can you share the code?