I know that Stalin was extremely paranoid and he had a ton of people shot for really tenuous reasons, but it seems to me that a lot of times it was completely random. People like Bukharin and Tuchachevesky never were a threat to Stalins power, but they were killed anyway. Same with Rykov.
First point - Stalin didn't pick all the victims of the purges. Even accounts that centre on Stalin's complicity acknowledge that there was a lot of denunciation going around and local scores being settled during the purges. Even drawing up the lists of arrests and signing off on them wasn't solely Stalin's job.
Sheila Fitzpatrick suggested the purges were driven to some extent by the party rank and file and general public, going after perceived/actual corrupt officials. J. Arch Getty suggests there were essentially many seperate purges occuring at once, resulting from rising political tensions, conflict between power bases/groups (e.g. centre vs local party organisations vs rank and file).
Getty probably takes his argument a bit too far, claiming Stalin was essentially undecieded and just shifting from position to position. He also depicts the purge running out of control... which it probably did. But he never really explains the trigger event for the major blood letting, the purge of the military leadership. In the "Origins of the Great Terror" he briefly suggests some military figures got mixed up in regional political machines (which were the 'real' target)... but that seems inadequete. Even the arrest of past political enemies could make a kind of sense (e.g. Bukharin, Rykov might have been in the way of consolidation, or called for restraint at the wrong times), but why the marshals (e.g. Tuchachevsky)? The military hadn't really featured in the pre-purge rhetoric which seemed to otherwise flag what was coming.
Stephen Kotkin is critical of claims that Stalin was simply crazy or paranoid. I think he has a point - if Stalin were that pathological he never would have lasted that long, and why would he ever stop the purges?
So tl;dr Stalin never 100% controlled the purges. As to why some of the upper echelons got arrested (i.e. people probably at least agreed with purging), there doesn't seem to be a satisfactory answer.