# Custom agent state Results so far only carry a `solution`. When an algorithm needs per-agent state (velocity, memory, a tag, ...), declare an agent with `cy.Attribute` and pass it to the optimizer. ```{literalinclude} _snippets/problem.py :language: python ``` ```python @cy.agent class Particle: velocity: cy.Attribute[float, ..., 0.0] @cy.optimizer(agent=Particle) class MyPSO: inertia: cy.Argument[float, (0.0, 1.5), 0.7] def generate(self, agent, solution): agent.velocity = self.rng.normal(0, 1, self.bounds.n_dims) return agent def evolve(self, epoch): for agent in self.population: agent.velocity = (self.inertia * agent.velocity + self.rng.normal(0, 1, self.bounds.n_dims)) agent.solution = agent.solution + agent.velocity ``` Overriding `generate` is optional; without it, each new agent gets the declared defaults. The hook receives the freshly created agent — declared defaults already applied, not yet evaluated — plus the sampled `solution`, and returns the agent to keep. --- Next: [Classic API and compilation](05-legacy-and-compilation.md).